iOS动态高度UITableViewCell和heightForRowAtIndexPath

bro*_*did 7 iphone objective-c uitableview ios

我在一个大项目中使用Autolayout来创建新的UITableViewCells.

我有一个TableView,其中每行的高度是自动计算的,我不使用委托函数heightForRowAtIndexPath.

我已经宣布估计行高:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

我的问题是:在另一个TableViewController上有很多UITableViewCells,我在编程上需要声明单元格的高度heightForRowAtIndexPath.我知道将所有单元格转换为使用唯一解决方案会更好,但在这个项目中有很多不同的单元格,所以我想使用一种解决方法并将动态计算的高度与自动布局和编程计算的行相结合高度.

这可能吗?

Hin*_*ndu 20

如果您使用的是iOS 8及更高版本,则无需动态计算高度.自动布局将为您完成所有工作.但如果使用低于IOS 8,则需要计算单元格高度.

对于IOS 8:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

并在控制器中添加以下代码:

tableView.estimatedRowHeight = 400.0
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

哪里estimatedRowHeight应该是您的细胞的最大高度.

谢谢