iOS UITableView高度简约手动计算并缓存

-(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 滚动更加流畅,目前尚未发现问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值