IOS 8 Self-Sizing Cells with Accessory

Using the simulator iOS 8.3 and Xcode 6.3.2.

I use the self-tuning cell technique in iOS 8, and it works terribly when the cell does not have an additional view, but it breaks when the cell has an additional view. I set label limits inside the cell:

UILabel constraints in a UITableViewCell

Then I use estimatedRowHeight and set rowHeight to UITableViewAutomaticDimension (here I omit tableView:numberOfRowsInSection: but it just returns 3 for this example):

 @implementation MyTableViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.estimatedRowHeight = 44.0f; self.tableView.rowHeight = UITableViewAutomaticDimension; } ... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath]; cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment "; return cell; } @end 

And bam, everything looks great:

UITableView looks fine

But when I add a helper view to the Storyboard without changing any code:

With accessory view

the first cell goes to hell. The Xcode view debugger tells me that the label height is 1785 points, and I had to use gif to show it here:

Huge cell

I notice that the other two cells are sized. I also note that when I rotate the simulator, the first cell changes correctly, including after it rotates back to the portrait.

Does anyone know why an accessory looks so bad, and what can I do to fix it? A sample project is available at http://github.com/UberJason/SelfSizingNightmare .

+7
ios iphone uitableview ios8
source share
2 answers

This is really a mistake. I sent your project to Apple. Answer from Apple.

enter image description here

+2
source share

This seems to be caused by a label inside the cell having numberOfLines = 0 .

Although I'm not sure why, it seems that adding an accessory to the cell leaves the automatic layout unable to calculate the height of the label the first time the table is loaded.

I was able to correct your example by adding the preferred maximum width to the label, which seems to be sufficient for the layout system to determine the height over time:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath]; // Give the layout system something to help it estimate the label height cell.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width; cell.titleLabel.text = @"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment "; return cell; } 

Alternatively, calling [tableView reloadData] on viewDidAppear also seems to fix it.

0
source share

All Articles