-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.count;
}
/// tableView刷新页面第一步 heightForRowAtIndexPath 代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id data = self.arr[indexPath.row];
// 当data.h 的高度 <= 0时,延迟调用刷新;因为tableView先走 heightForRowAtIndexPath 代理方法, 再走cellForRowAtIndexPath,所以第一次刷新页面肯定高度不存在,延迟的时间是为了走cellForRowAtIndexPath方法缓存高度,可以根据需要适当调整延迟调用刷新的时间
if (data.h <= 0)
{
[self performSelector:@selector(tableRload) withObject:nil afterDelay:0.5];
}
return data.h;
}
/// tableView刷新页面步第二步 cellForRowAtIndexPath 代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyCell description] forIndexPath:indexPath];
id data = self.arr[indexPath.row];
cell.data = data;
/// 在 cell 中实现 -(CGFloat)cellHeight; 的方法用于高度计算,cell中计算可以拿到所有想要的view的frame, 计算完保存在模型中,用于高度缓存
data.h = [cell cellHeight];
return cell;
}
-(void)tableRload
{
[self.tableView reloadData];
}
// 目的简便tableView高度计算(也就是不依靠第三方和autolayout,第三方存在更新问题,autolayout不适用复杂逻辑的高度计算),高度缓存会使tableView 滚动更加流畅,目前尚未发现问题