在UITableView 中 , 实现 UITableViewDelegate 的时候,一般需要实现 heightForRowAtIndexPath 来返回当前indexPath的cell 的高度。 但是很多时候 cell的高度不是固定的,这个时候单独去计算每个cell的高度就会很麻烦。现在 Apple 在 iOS 7.0 之后,提供了一个函数: estimatedHeightForRowAtIndexPath
只要在这个函数里面返回: UITableViewAutomaticDimension ,然后同样,在 heightForRowAtIndexPath 里面也返回 UITableViewAutomaticDimension ,这样就会自动计算cell的高度了,再也不需要单独每个cell去计算高度了。当然,cell本身的高度还是需要自己处理的。
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}