Operative
Operative

Reputation: 149

ILTableViewCell/UITableViewCell lifecycle

So I'm trying to rename a UILabel inside an instance of the UITableViewCell class from an instance of UITableView, however, the UILabel is not loaded by that time and so when I print it out in the debugger, it's nil.

So, instead I made a NSString inside the UITableViewCell and set that instead, which behaves expectedly because the debugger prints it out as what I set it.

Now my question is where in the lifecycle should I set the UILabel with the NSString that I successfully set

-(void) layoutSubviews
{
    [super layoutSubviews];
    self.fileLabel.text = self.fileName;
    NSLog(@"filelabel.text: %@", self.fileLabel.text);
}

layoutSubviews is not working is there any other method which I should use?

Upvotes: 0

Views: 1991

Answers (2)

user3539959
user3539959

Reputation: 383

Why don't you set it in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method? This method is supposed to be called when you are laying out the cell and this is also possibly where you would be adding the label to the cell.

Upvotes: 0

Bharat
Bharat

Reputation: 3007

If i understand your question- you are trying to add a label to a subclass of UITableViewCell, if so then use cellForRowAtIndexPath method like -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    YourCell *urCell = (YourCell *)cell;
    // Configure the cell...

   urCell.fileLabel.text = @"Some text";

    return cell;
}

Upvotes: 2

Related Questions