简体   繁体   中英

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:

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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:

在此输入图像描述

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.

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";

}

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM