我有一个UITableView,其中显示了一个自定义的单元格。 我的单元格中有两个标签和一个视图,如下面的图片所示。
我已经按照以下方式给左侧视图设定了约束
Item label constraints
center view constraints
right view constarints
我正在使用一个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;
}
问题是标签和表视图单元格的高度都没有增加。我已经解释了一切。当标签文本增加时,我想要使标签的尺寸增加,并且当标签尺寸增加时,单元格的高度也必须增加。