2016-06-27 3 views
0

Я пытаюсь создать представление, чтобы показать список предметов и цен, используя два UILabels в UIView.Создание стиля квитанции UIView

В моем UIViewController я вызываю свой subview LineItemView и передаю данные и возвращают UIView к основному виду, которое будет удерживать subviews.

Хотя мое представление возвращается пустым. Строки равны нулю.

After it is called

ViewController.m

#import "LineItemView.h" 
//... 
@property (weak, nonatomic) IBOutlet UIView *viewCharges; 
//... 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self loadData]; 

} 

- (void) loadData { 
    //Here we will fill in the viewCharges view 
    LineItemView * view = [[LineItemView alloc]init]; 
    view.linePrice = @"1.00"; 
    view.lineItem = @"Something"; 
    [self.viewCharges addSubview:view]; 

} 

LineItemView.h

@interface LineItemView : UIView { 
    UILabel * lblLineItem, *lblLinePrice; 
} 

@property (nonatomic,strong) NSString* lineItem; 
@property (nonatomic,strong) NSString* linePrice; 

LineItemView.m

#define LABEL_MINIMUM_HEIGHT   32 
#define VERTICAL_MARGIN    5 
#define HORIZONTAL_MARGIN   10 

@implementation LineItemView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self createUI]; 
     [self updateData]; 
     NSLog(@"\n\n\n Line Item: %@ Price: %@ \n\n\n", self.lineItem, self.linePrice); 

    } 
    return self; 
} 

-(void) createUI { 
    lblLineItem = [[UILabel alloc] initWithFrame:CGRectZero]; 
    lblLineItem.backgroundColor = [UIColor greenColor]; 
    [self addSubview:lblLineItem]; 

    lblLinePrice = [[UILabel alloc] initWithFrame:CGRectZero]; 
    lblLinePrice.backgroundColor = [UIColor yellowColor]; 
    [self addSubview:lblLinePrice]; 

} 

- (void) updateLayout { 
    lblLineItem.frame = CGRectMake(HORIZONTAL_MARGIN, VERTICAL_MARGIN, 300, 35); 
    lblLinePrice.frame = CGRectMake(lblLineItem.frame.origin.x + lblLineItem.frame.size.width + HORIZONTAL_MARGIN, VERTICAL_MARGIN, 80, 35); 
} 

- (void) updateData { 
    lblLineItem.text = self.lineItem; 
    lblLinePrice.text = [NSString stringWithFormat:@"%0.2f", [self.linePrice floatValue]]; 
} 

- (void) layoutSubviews { 
    [super layoutSubviews]; 
    [self updateLayout]; 
} 

Что я делаю неправильно? Если я хочу продолжить вызов LineItemView, как я могу убедиться, что он добавлен ниже предыдущего, и обеспечить, чтобы размер ViewCharges был перенастроен, чтобы соответствовать всем областям?

+0

Вам нужно позвонить 'updateData' после установки значения для' 'linePrice'and lineItem'. Но мой совет, используйте 'UITableView', он будет обрабатывать большие наборы данных лучше. – rckoenes

+0

Строки имеют значение null для init. –

+1

Да, и это правильно, потому что init вызывается при запуске представления: 'LineItemView * view = [[LineItemView alloc] init];' – rckoenes

ответ

0

Решение с использованием сеттеры:

@implementation LineItemView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self createUI]; 
    } 
    return self; 
} 

-(void) createUI { 
    lblLineItem = [[UILabel alloc] initWithFrame:CGRectZero]; 
    lblLineItem.backgroundColor = [UIColor greenColor]; 
    [self addSubview:lblLineItem]; 

    lblLinePrice = [[UILabel alloc] initWithFrame:CGRectZero]; 
    lblLinePrice.backgroundColor = [UIColor yellowColor]; 
    [self addSubview:lblLinePrice]; 

} 

- (void) updateLayout { 
    lblLineItem.frame = CGRectMake(HORIZONTAL_MARGIN, VERTICAL_MARGIN, 300, 35); 
    lblLinePrice.frame = CGRectMake(lblLineItem.frame.origin.x + lblLineItem.frame.size.width + HORIZONTAL_MARGIN, VERTICAL_MARGIN, 80, 35); 
} 

- (void) layoutSubviews { 
    [super layoutSubviews]; 
    [self updateLayout]; 
} 

- (void)setLineItem:(NSSTring *)lineItem { 
    _lineItem = lineItem; 
    lblLineItem.text = self.lineItem; 
} 

- (void)setLinePrice:(NSNumber *)linePrice { 
    _linePrice = linePrice; 
    lblLinePrice.text = [NSString stringWithFormat:@"%0.2f", [self.linePrice floatValue]]; 
} 
Смежные вопросы