Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Exactly Does layoutSubviews get called for custom UITableViewCell?

When exactly does layoutSubviews get called on a custom UITableViewCell in a UITableViewCells cellForRowAtIndexPath method? Below, I need layoutSubviews to be called AFTER I set the FiltersTableViewCellItem property. Do I have this set up correctly? I'd like to be able to use layoutSubviews because I heard it's better for performance.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"FiltersTableViewCell";
    FiltersTableViewCell *filtersTableViewCell = [[self dequeueReusableCellWithIdentifier:cellIdentifier] retain];
    FiltersTableViewCellItem *filtersTableViewCellItem = [[self.filtersTableViewCellItems objectAtIndex:[indexPath row]] retain];

    if (!filtersTableViewCell)
    {
        filtersTableViewCell = [[FiltersTableViewCell alloc] initWithFiltersTableViewCellItem:filtersTableViewCellItem];
        filtersTableViewCell.delegate = self;
    }
    else
    {
        filtersTableViewCell.filtersTableViewCellItem = filtersTableViewCellItem;
    }
    return [filtersTableViewCell autorelease];
}
like image 537
Ser Pounce Avatar asked Feb 01 '13 06:02

Ser Pounce


2 Answers

layoutSubviews is called at some point after tableView:willDisplayCell:, which is called after tableView:cellForRowAtIndexPath:. You can verify this by setting breakpoints in the relevant methods and seeing the order in which they get hit. For example, set a breakpoint at the end of tableView:cellForRowAtIndexPath:. Then add the following method to FiltersTableViewCell

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

and set a breakpoint there. Then run the app and see what happens.

like image 198
Timothy Moose Avatar answered Oct 10 '22 01:10

Timothy Moose


Try calling setNeedsLayout after setting your item.

filtersTableViewCell.filtersTableViewCellItem = filtersTableViewCellItem;
[filtersTableViewCell setNeedsLayout];
like image 37
Sigma4Life Avatar answered Oct 10 '22 02:10

Sigma4Life