您可以在 tableView:heightForRowAtIndexPath: 中设置高度。您可以在该方法中访问索引路径,因此可以访问向单元格提供数据的数组的索引。因此,您需要查看该数据,并进行确定单元格高度所需的任何计算,并返回该值。
编辑后:这是一个计算多行标签高度的示例。我创建了一个临时标签,用该行将包含的文本填充它,然后使用 sizeWithFont:constrainedToSize:LineBreakMode: 进行计算:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = _objects[indexPath.row][@"detail2"]; // this is the data I'm passing in the detail label
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(300, 300000) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat h = labelSize.height;
return h + 50; //50 is base height for my cell with only one line of text, determined empirically
}