1.tableViewCell自动变高
方案:很容易借助autolayout实现,只需要cell可以自身根据autolayout定出自身的高度即可。类似scrollView的ContentSize的定法。此处不多赘述。
Code:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;// 此值为一个大概值,类似于占位值
2.tableViewCell自动变高后可能出现的UI问题
2.1在加载更多数据(特别是reloadData后)可能出现的点击状态栏不能正常返回顶部,会差一些距离
原因:不明。可能的是自身对于预估的行高缓存有问题导致。
解决方法:缓存cell的高度
@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度所用字典
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
if(height)
{
return height.floatValue;
}
else
{
return 100;
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSNumber *height = @(cell.frame.size.height);
[self.heightAtIndexPath setObject:height forKey:indexPath];
}