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

标签 ios objective-c uitableview autolayout uistoryboard

编辑

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

工作项目: enter image description here

不工作的项目: enter image description here

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

已经搜索了网络以试图了解正在发生的事情,但由于某种原因,我的单元格高度不正确,直到我滚动到原型(prototype)单元格。

初始加载时: enter image description here

在滚动过每个单元格后: enter image description here

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

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

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

enter image description here

enter image description here

enter image description here

enter image description here

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

最佳答案

我终于找到了为什么它不起作用的问题。我下载了你上传到驱动中的项目,并在其中进行了修改。并发现您使用 compact Regular 大小类别导致错误的问题。将尺寸类转换为 Any, Any 后,它工作正常且完美。因此解决方案的关键是将 wCompact hRegular 更改为 wAny hAny。代码没有变化。

这是最初的答案。

详细答案:

1) 对于尺码等级,首先阅读 this苹果文件。

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

enter image description here

这表明您选择了特定的 View ,例如紧凑的宽度和常规的高度,这意味着此自动布局仅适用于特定的 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.

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

enter image description here

在这个 Size 类中,它工作正常。

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

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

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

关于ios - 动态 TableView 单元格高度在滚动之前不正确(iOS 8)-附上完整的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114994/

相关文章:

ios - 如何从另一个类调用一个 xib 文件,ios

ios - 是否可以使用 PayPal REST API 向卡客户端收费?

objective-c - reloadData 崩溃的 iOS 应用程序

ios - Stepper 更改后从单元格获取索引路径

ios - 在 Swift 3 中绘制 CGImage 的直方图

ios - 调试符号中的 Armv7s 仅当从最近的设备中拉出时?

objective-c - NSScanner 从 NSString 中删除子字符串引用

ios - MMdrawercontroller:打开左侧菜单时点击centerVC

iOS自定义按钮和像venmo一样的 ScrollView

iphone - UITableView 中的编辑模式