【发布时间】:2013-10-21 15:27:37
【问题描述】:
我正在尝试使用自动布局自动调整表格单元格的大小。 但似乎 TableView 忽略了高度限制。
我知道“tableView: heightForRowAtIndexPath:”可以做到这一点。
但是如何避免使用高度硬编码? 还有其他方法吗?
屏幕截图 1:
屏幕截图 2:
【问题讨论】:
标签: objective-c uitableview storyboard autolayout
我正在尝试使用自动布局自动调整表格单元格的大小。 但似乎 TableView 忽略了高度限制。
我知道“tableView: heightForRowAtIndexPath:”可以做到这一点。
但是如何避免使用高度硬编码? 还有其他方法吗?
屏幕截图 1:
屏幕截图 2:
【问题讨论】:
标签: objective-c uitableview storyboard autolayout
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static UITableViewCell *cell;
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier: @"YourCell"];
}
[self configureCell:cell forIndexPath:indexPath]; // making text assignment and so on
return [cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height + 1.0f;
}
【讨论】:
tableView:heightForRowAtIndexPath: 为 tableView 中的每一行调用。如果 tableView 中有多个单元格,这将对性能产生巨大影响。
t:heightForRowAtIndexPath: 应该尽可能快地运行。
cellForRowAtIndexPath 外面打电话给dequeueReusableCellWithIdentifier,哇...不要那样做。它搞乱了UITableView 的设置方式。