简体   繁体   中英

Dynamic TableView Cell Heights Incorrect Until After Scrolling (iOS 8) - full screenshots attached

EDIT

Built a new sample project from scratch and the dynamic tableview cell height is working flawlessly. I then tried to replicate by trimming down my project to it's bare minimum and it's STILL broken (first few cells buggy). Attaching full project for both working and not working examples. Project/code literally looks 99% identical and for the love of me can't figure out where the 1% difference is. The only thing that is popping out at me is that I used different size classes (wAny hAny vs wCompact hRegular but I can't imagine that would do anything given I'm testing in portrait only

Working project: 在此输入图像描述

Not working project: 在此输入图像描述

WORKING (new project from scratch):
https://drive.google.com/file/d/0B_EIkjmOj3ImWXZjVFZMYXZmVGc/view?usp=sharing

NOT WORKING (my project, cleaned up to it's bare minimum)
https://drive.google.com/file/d/0B_EIkjmOj3ImMGRNOXU2RlNkWEk/view?usp=sharing

Have scoured the web trying to understand what is going on, but for some reason my cell heights are incorrect until I scroll past the prototype cells.

Upon initial load: 在此输入图像描述

And after scrolling past each cell: 在此输入图像描述

Background colors: cell.postTextLabel.backgroundColor = [UIColor orangeColor]; subView.backgroundColor = [UIColor blueColor]; contentView.backgroundColor = [UIColor brownColor]; cell.postTextLabel.backgroundColor = [UIColor orangeColor]; subView.backgroundColor = [UIColor blueColor]; contentView.backgroundColor = [UIColor brownColor];

Not doing anything too fancy here: just a prototype cell, a subview, and three labels (username, timestamp, text).

Below screenshots highlight my constraints in Storyboard:

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

I pull data from Parse, and am reloading my data while setting tableView layout

[self.tableView setNeedsLayout]; [self.tableView layoutIfNeeded]; [self.tableView reloadData];

And lastly my cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self postCellAtIndexPath:indexPath];
}


- (UITableViewCell *)postCellAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *PostCellIdentifier = @"PostCell";
    PostCell *cell = (PostCell *)[self.tableView dequeueReusableCellWithIdentifier:PostCellIdentifier forIndexPath:indexPath];

    [self configurePostCell:cell atIndexPath:indexPath];

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];



    return cell;
}

Finally i found the problem why it is not working. I downloaded the Projects which were uploaded to drive by you, and do modifications in it. And found the issue that you use compact Regular size classes which causes error. After converting your size classes to Any, Any it works fine and perfect.so the key of the solution is change wCompact hRegular to wAny hAny. There is no change in code.

That was initial answer.

Detailed Answer :

1) for the size classes first read this document of apple.

2) In your broken demo the size classes are selected as given below :

在此输入图像描述

This indicates that you selected specific view like compact width and regular height means this auto layout will only work in specific iPhone portrait orientation.

Compact Width | Regular Height combination specifies layout changes that apply only to sizes resembling iPhone devices in portrait orientation.

The control wCompact hRegular indicates the compact width and regular height size classes.

I used below size classes and also in your working project i can be able to see below size classes :

在此输入图像描述

In this Size classes it's working fine.

I don't know is it apple's bug or what. !!!

If you want to support only portrait mode and only iPhones then you can choose iPhone in Development Info -> devices. and for orientation you can choose Portrait and upside down in Device orientation.

Hope this helps you to figure out the problem.

I have met the same issue, just try to use a larger estimatedRowHeight which can contain your content. SDK seems do something wrong when using self-sizing cell.

Just set your row height programatically.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
 {
   return rowHieght;
 }

I also had the same issue, how-to-add-uiview-in-uitableviewcell

You also have custom cell, so use the following function in customcell class, in your case it is PostCell.m.

- (void)layoutSubviews 
{
    [super layoutSubviews];
    // in my case i only had set frame here while my other  
    // declarations were in init function, so it solve my problem
}

It solved my same problem, hope it will solve your too, but you have to think it in your own scenario.

First of all you have to set your Detail label line 0 then give leading, trailing and bottom constraints.

in viewDidLoad method add:

tableview.estimatedRowHeight = 50.0 //(your estimated row height)
tableview.rowHeight = UITableViewAutomaticDimension

then return the row height in tableview delegate get method cell row height

In Your code

[cell layoutIfNeeded]
[cell updateContraintsIfNeeded]

are not needed so remove it

for my issue, I set the UILabel font in

layoutSubviews:

thats causing the problem.

after I moved the code to other place, then it works just fine

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