正确的解决方案是:
添加@property (strong, nonatomic) MyCell* prototypeCell;
到控制器。
创建一个getter:
- (MyCell*) prototypeCell {
if (!_prototypeCell) {
_prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell"];
}
return _prototypeCell;
}
移动的所有代码,相关单元配置从cellForRow
到Cell类(你可以在这里看看:How to access properties in multiple custom 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
}