Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell with auto layout and accessory view

I have a custom table view cell that uses auto layout and has a disclosure indicator as an accessory view. The cell size of the cells on the screen are completely wrong when first displayed:

As you can see the cell is taking about a 1.5 screens worth of space:

enter image description here

However if I rotate the device and rotate back it looks fine:

enter image description here

As you can see here, I've done nothing complicated:

enter image description here

I have a very NON-IDEAL workaround which is to do:

-(void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

But that obviously causes a 'flash' when you first see the screen. In a more complicated scenario the flash is far more obvious.

I have another workaround but this causes an auto layout exception:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];
    cell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines";
    [cell.basicLabel layoutIfNeeded];
    return cell;
}

Exception:

enter image description here

At least this method doesn't give me UI flashing.

If I remove the accessory view it actually works perfectly fine.

UPDATE: I've added a sample project to github: https://github.com/fwaddle/TableCellAccessoryTest

UPDATE #2: Turns out another work around this bug is to layout the cell in code. I just tried doing the same thing in code and it didn't throw the warning and worked fine. Looks like an IB bug.

Any ideas how to work around this issue? Thanks.

like image 483
Mof Avatar asked May 08 '15 02:05

Mof


1 Answers

Implement the following delegate method as this solved the problem for me.

- (void)tableView:(UITableView *)tableView   
willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath*)indexPath

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

Add this code to your tableViewController:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell   forRowAtIndexPath:(NSIndexPath *)indexPath{

    BasicCell *basicCell = (BasicCell *)cell;
    basicCell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines";

}
like image 178
Michael Avatar answered Sep 19 '22 14:09

Michael