【发布时间】:2015-06-18 06:30:11
【问题描述】:
我在自定义单元格中有一个 UIImageView。如果单元格中没有要显示的图像,则单元格高度应自动减小。(在这种情况下,ImageView 高度应为零)。如何在 xib 中为此指定约束?
【问题讨论】:
-
图像的高度是否不同,还是始终相同?您是否添加了从图像顶部到单元格顶部的约束,并且对于底部也是如此?
标签: ios uiimageview autolayout
我在自定义单元格中有一个 UIImageView。如果单元格中没有要显示的图像,则单元格高度应自动减小。(在这种情况下,ImageView 高度应为零)。如何在 xib 中为此指定约束?
【问题讨论】:
标签: ios uiimageview autolayout
您的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
}
【讨论】:
你的 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. 的很好的示例代码
【讨论】: