Mof Mof - 4 months ago 16
iOS Question

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.

Mof Mof
Answer

So even creating the constraints in code sometimes doesn't fix this issue. It seems you need a few more changes. In your custom table cell add the following especially if you're changing the accessory type depending on the contents of the cell (e.g. Checkmark):

-(void) setAccessoryType:(UITableViewCellAccessoryType)accessoryType {
  [super setAccessoryType:accessoryType];
  [self setNeedsUpdateConstraints];
}

Also remove the prototype cell in the storyboard and register your class instead:

-(void) viewDidLoad {
  [super viewDidLoad];
  [self.tableView registerClass:[MyCustomCell class] forCellReuseIdentifier:@"MyCustomCell"];
}

I occasionally find that I still need to force labels (especially multi-line labels it seems) to re layout in cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell" forIndexPath:indexPath];
  cell.customLabel.text = .....
  [cell.customLabel layoutIfNeeded];
  return cell;
}

All of the above fixed my issues so I hope they're of use to others and you don't waste a large amount of time on this.

I still haven't heard anything back from Apple about the bug report but I assume that's pretty normal.