正确的解决方案是:
添加@property (strong, nonatomic) MyCell* prototypeCell;
到控制器。
创建一个吸气剂:
- (MyCell*) prototypeCell {
if (!_prototypeCell) {
_prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell"];
}
return _prototypeCell;
}
将所有与单元格配置相关的代码移动到单元cellForRow
格类(您可以在这里查看:如何访问多个自定义 tableviewcells 中的属性)
修改 heightForRow:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height+1;
}
适用于自动布局。如果您没有启用自动布局 - 您可以手动计算。
要提高 iOs7 的性能,请使用:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 300; // or any number based on your estimation
}