【问题标题】:Dynamic height in cell Issue in iPhone6 and iPhone6 plusiPhone 6 和 iPhone 6 plus 中的单元格动态高度问题
【发布时间】:2015-02-22 08:27:06
【问题描述】:

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

当我使用 iPhone 6 或 iPhone 6 plus 运行我的应用程序时,我遇到了如下图所示的意外行为,该图显示了 iPhone 6 模拟器

iPhone 6 plus 模拟器

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

iPhone 4、4s、5 和 5s 笔尖文件快照

iPhone 6 和 iPhone 6 plus 自定义单元笔尖文件快照

我还使用了以下代码 sn-p,其中单元格已使用动态高度调整大小。该代码适用于 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
}

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

【问题讨论】:

    标签: ios uilabel autolayout nslayoutconstraint custom-cell


    【解决方案1】:

    根据您对 iPhone6,6 Plus 中单元格高度的问题,您需要在下面写下几行。

    tableView.estimatedRowHeight = 44.0 
    tableView.rowHeight = UITableViewAutomaticDimension 
    

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

    【讨论】:

    • 对于 iPhone 6 和 6 plus,似乎必须将 estimatedRowHeight 设置为一个值,而不仅仅是 UITableView.automaticDimension
    【解决方案2】:

    你有两个不同的 xib 用于同一个单元格的原因吗? AutoLayout 的全部意义在于,单个布局可以足够灵活以支持所有屏幕尺寸。

    使用自动调整单元格大小时,要记住的关键事项是:

    • tableView.estimatedRowHeight = <estimated_value>
    • tableView.rowHeight = UITableViewAutomaticDimension
    • 确保您的UITableViewCell 子类中有明确的布局,并且所有元素都是.contentView 的子视图

    您的布局看起来非常适合UIStackView。我建议使用包含 3 个标签的垂直 UIStackView。然后是水平的UIStackView,包含之前的UIStackViewUIImageView。您需要创建的唯一NSLayoutConstraints 是将外部UIStackView 固定到.contentView 的所有侧面

    有关UIStackViews 的更多信息,请查看我的博文:

    http://www.raizlabs.com/dev/2016/04/uistackview/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      相关资源
      最近更新 更多