滚动后动态 TableView 单元格高度不正确 (iOS 8) - 附上完整的屏幕截图

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

编辑

从头开始构建了一个新的示例项目,动态表格视图单元格高度运行完美。然后,我尝试通过将我的项目削减到最低限度来进行复制,但它仍然存在问题(前几个单元有问题)。为工作示例和非工作示例附加完整项目。 Project/code 字面上看起来 99% 相同,看在我的份上无法弄清楚 1% 的差异在哪里。唯一突然出现的是我使用了不同的尺寸 类(wAny hAny vs wCompact hRegular 但我无法想象这会做任何事情,因为我只在纵向测试

工作项目:

不工作的项目:

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

已在网上搜索以试图了解发生了什么,但由于某种原因,我的单元格高度不正确,直到我滚动到原型单元格。

初始加载时:

滚动经过每个单元格后:

背景颜色: cell.postTextLabel.backgroundColor = [UIColor orangeColor]; subView.backgroundColor = [UIColor blueColor]; contentView.backgroundColor = [UIColor brownColor];

这里没有做任何太花哨的事情:只有一个原型单元格、一个子视图和三个标签(用户名、时间戳、文本)。

下面的截图突出了我在 Storyboard 中的约束:

我从 Parse 中提取数据,并在设置 tableView 布局时重新加载我的数据

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

最后是我的 cellForRowAtIndexPath 方法:

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

我遇到了同样的问题,只是尝试使用更大的 estimatedRowHeight 来包含您的内容。 SDK 在使用自调整单元格时似乎做错了。

只需以编程方式设置行高。

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

我也遇到了同样的问题,

您还有自定义单元格,因此在自定义单元格 class 中使用以下函数,您的情况是 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
}

它解决了我同样的问题,希望它也能解决你的问题,但你必须根据自己的情况考虑。

首先,您必须设置详细信息标签行 0,然后给出前导、尾随和底部约束。

viewDidLoad方法中添加:

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

然后returntableview中的行高delegate get method cell row height

在你的代码中

[cell layoutIfNeeded]
[cell updateContraintsIfNeeded]

不需要所以删除它

对于我的问题,我在

中设置了 UILabel 字体
layoutSubviews:

这就是问题所在。

我把代码移到其他地方后,就可以正常工作了

我终于找到了为什么它不起作用的问题。我下载了你上传到驱动中的项目,并在其中进行了修改。并发现您使用 compact Regular size 类 导致错误的问题。将尺寸 类 转换为 Any、Any 后效果很好 perfect.so 解决方案的关键是更改 wCompact hRegularwAny hAny。 代码没有变化。

这是最初的回答。

详细答案:

1) 对于尺寸类首先阅读苹果的this文档。

2) 在您损坏的演示中,尺寸 类 的选择如下:

这表明您选择了特定的视图,例如紧凑宽度和常规高度,这意味着此自动布局仅适用于特定的 iPhone 纵向。

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.

我使用了以下尺寸 类 并且在您的工作项目中我可以看到以下尺寸 类 :

在这个尺寸 类 中它工作正常。

不知道是苹果的bug还是什么。 !!!

如果您只想支持纵向模式并且只支持 iPhones 那么您可以在开发信息 -> 设备中选择 iPhone。对于方向,您可以在设备方向中选择纵向和倒置。

希望这可以帮助您解决问题。