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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 05:36:09

问题


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

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

回答1:


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.




回答2:


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.




回答3:


Just set your row height programatically.

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



回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/30114994/dynamic-tableview-cell-heights-incorrect-until-after-scrolling-ios-8-full-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!