【问题标题】:custom cell height from UIImageView size?UIImageView 大小的自定义单元格高度?
【发布时间】:2015-06-18 06:30:11
【问题描述】:

我在自定义单元格中有一个 UIImageView。如果单元格中没有要显示的图像,则单元格高度应自动减小。(在这种情况下,ImageView 高度应为零)。如何在 xib 中为此指定约束?

【问题讨论】:

  • 图像的高度是否不同,还是始终相同?您是否添加了从图像顶部到单元格顶部的约束,并且对于底部也是如此?
  • 嗨,请看这个问题:stackoverflow.com/questions/30896856/…

标签: ios uiimageview autolayout


【解决方案1】:

您的UITableView 数据源应代表此图像的存在。 您应该使用以下委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource[indexPath.rox].imageExists) {
        return 50.0; // Change to your value with image
    }
    return 10.0; // Change to your value without image
}

【讨论】:

【解决方案2】:

你的 UITableViewDelegate 应该实现 tableView:heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // add your condition here if image exit or not and set cell size dynamically
    return [indexPath row] * 20;
}

您可能希望使用NSString's sizeWithFont:constrainedToSize:lineBreakMode: 方法来计算行高,而不是仅仅在 indexPath 上执行一些愚蠢的数学运算。

这是dynamic size cell. 的很好的示例代码

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2011-04-13
    相关资源
    最近更新 更多