Josh Kahane Josh Kahane - 1 year ago 144
Objective-C Question

Static UITableView Cell Property Modification

I have a static

setup in my storyboard. I have subclassed some cells and added this property:

@property (nonatomic, strong) UILabel *label;

I also have code to add the label to my cell in the
method. My label shows in my cell without an issue. I can see the label saying 'PLACEHOLDER' but it is not updated to say the text I set in

In the
method, I have the following:

APInfoTableViewCell *cell = (APInfoTableViewCell*)[super tableView:tableView cellForRowAtIndexPath:indexPath];

[cell.label setText@"Hello World"];

However, this will not update the text in my label. The label is actually nil. Please help!!!



Here is the full code...


@property (nonatomic, strong) UILabel *infoLabel;

[super layoutSubviews];

[self initUI];

//add long press to show info label
_infoLabel = [UILabel new];
[_infoLabel setText:@"PLACEHOLDER"];
[_infoLabel setFont:[UIFont fontWithName:@"Avenir-Medium" size:10.0f]];
[_infoLabel setTextAlignment:NSTextAlignmentCenter];
[_infoLabel setTextColor:[UIColor apText]];
[_infoLabel setAlpha:1.0f];
[_infoLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:_infoLabel];

NSArray *infoConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[infoLbl]-40-|" options:0 metrics:nil views:@{ @"infoLbl" : _infoLabel }];
[self addConstraints:infoConstraintH];
NSArray *infoConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[infoLbl]-10-|" options:0 metrics:nil views:@{ @"infoLbl" : _infoLabel }];
[self addConstraints:infoConstraintV];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
APInfoTableViewCell *cell = (APInfoTableViewCell*)[super tableView:tableView cellForRowAtIndexPath:indexPath];

[cell.infoLabel setText@"Hello World"];

return cell;

Answer Source

If you are using storyboard, and you don't want to add IB outlet then call your initUI method from awakeFromNib method. If you are not using storyboard implement - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier method and call it from there.

awakeFromNib method is called once the cell(or any UIView subclass) is instantiated from nib(either from storyboard or from xib). And its called only once in the lifetime of the view

Note: This will not get called if you are creating a view subclass programmatically.

As per documentation

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

That means, use this method to do any additional lay outing of already added. This method is called when some frame/autolayout constraint changes happens.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download