【发布时间】:2023-03-28 14:15:01
【问题描述】:
我有一个 UITableView,我在其中显示一个自定义单元格。我的单元格有两个标签和一个视图,如下图所示。
我已经给了这样的左视图约束
项目标签约束
中心视图约束
右视图约束
我正在使用 bean 类来存储两个标签的数据并将该 bean 对象添加到一个数组中。我在 heightForRowAtIndexPath 中使用以下代码。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Calculate a height based on a cell
if(!self.customCell) {
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"thirdcell"];
}
// Configure the cell
Instrument *inst=[arr_instSpecs objectAtIndex:indexPath.row];
self.customCell.label_item.text=inst.item;
self.customCell.label_desc.text=inst.desc;
// Layout the cell
[self.customCell layoutIfNeeded];
// Get the height for the cell
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// Padding of 1 point (cell separator)
CGFloat separatorHeight = 1;
return height + separatorHeight;
}
问题既不是标签的高度增加,也不是表格视图单元格的高度。我已经解释了一切。我想在标签文本增加时增加标签大小,并且当标签大小增加时,单元格的高度必须增加。
【问题讨论】:
-
您的问题不清楚您想要实现什么。仍然从您的代码中,您通过计算返回高度而不是返回 UITableViewAutomaticDimension。
-
关于 SO 上的类似场景有很多答案,这是stackoverflow.com/questions/19215199/…之一
-
我已经解释了一切。我想让标签的大小在标签文本增加时增加,并且当标签大小增加时,单元格的高度必须增加。
-
'Item' 和 'Description' 都是 UILabel 类吗?您的单元格中是否只有两个标签?
-
您提到您在单元格中也有一个视图,它在哪里?它的用途是什么?
标签: ios objective-c swift uitableview row-height