Firemarble Firemarble - 5 months ago 15
iOS Question

Creating a receipt style UIView

I am attempting to create a view to show a list of items and prices using two UILabels in a UIView.

In my UIViewController I call my subview LineItemView and pass data, and return the UIView to the main view that will hold the subviews.

Though my view is returning empty. The strings are null.

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


What am I doing wrong?
If I want to continue calling LineItemView, how do I ensure that it is added below the previous one and ensure the viewCharges size is readjusted to fit all the subviews?

Answer

Solution using setters:

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