简体   繁体   中英

Content of custom UITableViewCell dynamic height in iOS8 has different widths

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 following 在此处输入图片说明

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视图的约束 labelview's constraints 在此处输入图片说明 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!

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.

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

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