关于UITableView的重用机制浅谈

以前做过些项目,里面难免会遇到图文混排,Cell自适应高度的问题,当用到这些的时候,我们的一般做法是在cell的高度返回代理方法中算出Cell的高度,然后返回cell的高度.这其中就用到了cell得重用机制,于是想写一些关于Cell的重用机制的一些自己的理解.首先写了一个测试的Demo,贴代码得意

@implementation ViewController
{
    UITableView *_table;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createUI];
}
- (void)createUI
{
    _table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_table];
    _table.dataSource = self;
    _table.delegate = self;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"row");
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%d",arc4random()%100+1];
    NSLog(@"cell");
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"heigth");
    return 100;
}

运行,打印结果如下:

2015-12-16 20:04:44.994 TestTableview[2728:2226549] row
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.996 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.997 TestTableview[2728:2226549] heigth
2015-12-16 20:04:44.999 TestTableview[2728:2226549] row
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.000 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] row
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.009 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.011 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.013 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.021 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.022 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.023 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.023 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.024 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.025 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.026 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.026 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.027 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.027 TestTableview[2728:2226549] heigth
发现Row被执行了三次,前两次为什么执行,目前还没有弄懂 委屈,不过这个暂时不重要,我们开最后row这段

2015-12-16 20:04:45.008 TestTableview[2728:2226549] row
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.008 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.009 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.011 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.013 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.021 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.022 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.023 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.023 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.024 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.025 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.026 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.026 TestTableview[2728:2226549] heigth
2015-12-16 20:04:45.027 TestTableview[2728:2226549] cell
2015-12-16 20:04:45.027 TestTableview[2728:2226549] heigth
这里面height被执行了16次,前面执行了10次,然后和cell交替执行了6次,加上代码

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sometime) userInfo:nil repeats:NO];

- (void)sometime
{
    NSLog(@"visablecells count=%lu",_table.visibleCells.count);
}
打印结果

2015-12-16 20:20:38.562 TestTableview[2741:2230162] visablecells count=6
发现刚好可见的cells刚好是6个,而总过有10个cell,所以可以推断,tableview首先计算出总共数量的cell的高度(10个),这里是估算tableview的countofsize,然后先复用cell,根据计算cell的高度,直到cell将tableview尺寸(这里是整个屏幕)是塞满(visablecells这里是6个),滚动的时候,消失的visablecell(滚出tableview尺寸范围)将会进入复用池,当需要下个cell滚出来时,会先复用cell,在调用返回高度的方法确定cell的高度,这个复用过程就完成了.这就是cell自适应高度时,为什么在自定义cell里面根据内容算出cell的高度,并在代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
中返回cell的高度了.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值