ios - iPhone 6 和 iPhone 6 plus 中的单元格动态高度问题

标签 ios uilabel autolayout nslayoutconstraint custom-cell

我正在实现一个功能,其中我的单元格的所有标签都根据每个标签包含的文本调整大小。我在 RayWanderlich tutorial 的帮助下实现了这个功能。并且它在装有 iOS 8.1 的 iPhone 4s、5s 和 5s 上运行良好。如下图所示,其中显示了 带有 iOS 8.1 的 iPhone 5s 模拟器

enter image description here

当我在 iPhone 6 或 iPhone 6 plus 上运行我的应用程序时,出现了如下图所示的意外行为,其中显示了 iPhone 6 模拟器

enter image description here

iPhone 6 plus 模拟器

enter image description here

我创建了两个自定义单元格 Nib 文件,一个用于 iPhone 4、4s、5 和 5s,第二个用于 iPhone 6 和 6 plus。由于我是自动布局的新手,所以我设置了与两个 Nib 相同的约束。我在下面的图片中显示了两个带有设置约束的自定义单元格 Nib 文件。

iPhone 4、4s、5 和 5s nib 文件快照

enter image description here

iPhone 6 和 iPhone 6 plus 自定义单元格 nib 文件快照

enter image description here

我还使用了以下代码片段,其中单元格已使用动态高度调整大小。该代码适用于 iPhone iPhone 4(适用于 iOS 7)、4s、5 和 5s(适用于 iOS 7 和 iOS 8 和 8.1),但不适用于 iPhone 6 和 iPhone 6 plus。

代码片段

- (CGFloat)calculateHeightForConfiguredSizingCell_3:(MemberListTableViewCell_WithoutImage_3_iPhone *)sizingCell {


    sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tblView.frame), CGRectGetHeight(sizingCell.bounds));

    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    //CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultHigh];

    return size.height + 1.0f; // Add 1.0f for the cell separator height
}

请给我一个正确的解决方案,我在哪里出错了。我已经浏览了很多东西,但无法找到解决这个问题的正确方法。您的帮助将不胜感激。在此先致谢。

最佳答案

根据你的iPhone6,6 Plus的cell height的问题你需要写在下面几行。

tableView.estimatedRowHeight = 44.0 
tableView.rowHeight = UITableViewAutomaticDimension 

使用自动布局的动态单元格大小 引用:http://www.appcoda.com/self-sizing-cells/

关于ios - iPhone 6 和 iPhone 6 plus 中的单元格动态高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633180/

相关文章:

ios - 将方法从 Objective-C 转换为 Swift 时的区别

ios - UILabel 自动布局切割底部像素

ios - 在纵向和横向中使用 AutoLayout

ios - 自动布局 VFL : align all trailing in an horizontal constraint

ios - UIS滚动中心图像

ios - 如何检索用于创建 iOS MapKit MKPolygonView 的坐标?

ios - 如何快速计算文件夹引用中的图像数量

ios - 当文本长于标签的宽度时,如何使 UILabel 文本在末尾显示省略号

ios - 在单个标签中使用多种字体颜色

swift - iOS Swift 3 - 自动布局,根据文本或 View 大小更改高度