计算tableView不等高cell高度的几种方法

文章从简书搬家到掘金

这里利用heightForRowAtIndexPath:方法计算不等高cell的高度,在使用这个方法之前要明确这个方法的调用时间以及调用次数:

  • 1.每当reloadData时,有多少条数据,就会调用多少次这个方法(比如一共有80条数据,就会调用80次这个方法)
  • 2.每当有cell出现时,就会调用一次这个方法
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}
复制代码

  • 用于描述cell的xib

####计算不等高cell高度的第一种方法:估算高度

  • 1.在viewDidLoad方法中设置cell的估算高度
(void)viewDidLoad {
    [super viewDidLoad];
    //设置估算高度
    self.tableView.estimatedRowHeight = 170;
}
复制代码
  • 2.在heightForRowAtIndexPath:方法中计算cell的高度
//定义间距
#define WYLMargin 10
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    
    //定义cell的高度
    CGFloat cellHeight = 0;
    
    //图片+间距
    cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    cellHeight += 35 + WYLMargin;
    NSLog(@"heightForRowAtIndexPath-----%zd",indexPath.row);
    
    return cellHeight;
}
复制代码
  • 3.利用估算高度计算cell高度的优点和缺点
  • 优点: 1.减少heightForRowAtIndexPath方法的调用次数 2.可以让暂时看不见的cell的高度延迟计算
  • 缺点: 1.tableView的contentSize不太准确 2.在tableView滑动过程中,滚动条的长度会变来变去,可能会有跳跃效果 3.cell从缓存池中加载时又会重新计算它的高度

####计算不等高cell高度的第二种方法:定义字典保存cell高度

  • 1.定义存储cell高度的字典
/** 存储cell高度的字典*/
@property (nonatomic ,strong) NSMutableDictionary *cellHeightDict;
/** 懒加载存储cell高度的字典*/
-(NSMutableDictionary *)cellHeightDict
{
    if (_cellHeightDict == nil) {
        _cellHeightDict = [NSMutableDictionary dictionary];
    }
    
    return _cellHeightDict;
}
复制代码
  • 2.在heightForRowAtIndexPath:方法中计算cell的高度
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    
    //用模型的地址作为字典的key
    NSString *key = [NSString stringWithFormat:@"%p",item];
    
    //取出模型对应的cell的高度
    CGFloat cellHeight = [self.cellHeightDict[key] doubleValue];
    
    //如果cellHeight有值,就是cell的高度已经计算过了,直接返回
    if (cellHeight) return cellHeight;
    
    //如果cell的高度没有计算过,就计算cell高度
    //图片+间距
    cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    cellHeight += 35 + WYLMargin;

    //将计算过的cell高度保存到字典中
    self.cellHeightDict[key] = @(cellHeight);
    NSLog(@"heightForRowAtIndexPath----%zd",indexPath.row);
    
    return cellHeight;
}
复制代码
  • 3.利用字典保存cell高度的优缺点
  • 优点: 当cell重新加载到tableView中的时候,不会重复计算cell的高度
  • 缺点: 代码太过繁琐

####计算不等高cell高度的第三种方法:在模型中定义cellHeight属性

  • 1.在模型.h文件中增加cellHeight属性
#import <Foundation/Foundation.h>
@interface WYLTopicItem : NSObject
/** 根据当前模型数据计算出来的cell高度*/
@property (nonatomic ,assign) NSInteger cellHeight;
@end
复制代码
  • 2.在模型.m文件中实现cellHeight的getter方法
#import "WYLTopicItem.h"
@implementation WYLTopicItem
-(NSInteger)cellHeight
{
    //如果cellHeight已经计算过,就直接返回
    if (_cellHeight) return _cellHeight;
 
    //图片+间距
    _cellHeight += WYLMargin + 35 + WYLMargin;
    
    //label的高度
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
    CGFloat labelHeight = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    _cellHeight += labelHeight + WYLMargin;
    
    //底部导航栏的高度
    _cellHeight += 35 + WYLMargin;
    
    return _cellHeight;
}
@end
复制代码

-3.在heightForRowAtIndexPath:方法中计算cell的高度

//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取对应的模型
    WYLTopicItem *item = self.topicItemArray[indexPath.row];
    return item.cellHeight;
}
复制代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值