0

I met this issue after I upgraded xcode to 6.3. The former build compiled using xcode 6.2 doesn't have this issue and works great.

The issue is: After switch to other tab and back, or back from push segue, the width of most cells will shrink a little bit. But some don't. And it looks like followingenter image description here

The label content in 2nd line of cell has the correct width. And all cells display content with the correct width after first launch. In above image, there are two uitableviewcells. content view : blue; background view: light green; content Label: purple;
The bg view and contentLabel in 1st cell shrinked. That's what I don't want.

I use storyboard to set the constraints. bg view's constraints. bg view's contraints labelview's constraints enter image description here I also tried to cache the cell height as per this question UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift), but it doesn't work.

//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] floatValue];
    if (height) {
        return height;
    } else {
        return UITableViewAutomaticDimension;
    }

}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:@"%ld", indexPath.row]];
}

Thanks for your help!

Community
  • 1
  • 1
DianeZhou
  • 171
  • 1
  • 8

2 Answers2

0

Implement DataSource Method for Tableview WillDisplayCell

Or you can implement in cell class (if it's custom cell) method - (void)awakeFromNib and there set cell frame.

Anton
  • 3,102
  • 2
  • 28
  • 47
0

Well you are not setting the Horizontal Space Constraint for the view on top of your contentView. You are only setting Horizontal Space Constraint for your label and its superview.

Do this

  1. Select the view which contains the label
  2. Editor > Pin > Leading Space to SuperView
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51