创建收据样式UIView

时间:2016-06-27 14:12:13

标签: ios objective-c iphone uiview uiviewcontroller

我正在尝试使用UIView中的两个UILabel创建一个显示项目和价格列表的视图。

在我的UIViewController中,我调用我的子视图 LineItemView 并传递数据,并将UIView返回到将保存子视图的主视图。

虽然我的观点是空洞的。字符串为空。

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大小以适合所有子视图?

1 个答案:

答案 0 :(得分:0)

使用setter的解决方案:

@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]];
}