zoukankan      html  css  js  c++  java
  • UITableview delegate dataSource调用探究

    UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用。我们大多数人可能知道当reloadData这个方法被调用时,delegate和dataSource就会被回调,但是其中具体的细节,可能很多人不会去探究。
    我最近有兴趣来探讨这个问题是因为我最近遇到过dataSource中有的方法被调用,但是有的方法没有被调用的情况,同时你会发现当tableview被add到一个superView的时候,也会触发了reloadData一样的回调。那么这两个委托究竟是怎么执行的呢?

    • 我们首先来看看苹果文档对reloadData的描述

      Call this method to reload all the data that is used to construct the table, 
      including cells, section headers and footers, index arrays, and so on. For 
      efficiency, the table view redisplays only those rows that are visible. It adjusts 
      offsets if the table shrinks as a result of the reload. The table view’s delegate or 
      data source calls this method when it wants the table view to completely reload 
      its data. It should not be called in the methods that insert or delete rows, 
      especially within an animation block implemented with calls to beginUpdates and 
      endUpdates.

      大致的意思就是说reload这个方法是用来构建table的,包括cell、section,而且只会对可见的行进行重新的绘制,当tableview想要完整的加载数据时,delegate和data source会调用此方法。增加删除行,尤其是需要block动画的时候不用用它。
      从这里只能看出个大概,并没有解释调用的原理。

    • 那么让我们先写一个最基本的tableview实现,然后对delegate和data source的回调设置一下断点看看。

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        tableView.delegate = self;
        tableView.dataSource = self;
    
        [self.view addSubview:tableView];
    //    [tableView reloadData];
    //    [tableView layoutSubviews];
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 60;
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 20;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellID = @"cellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = @"哈哈";
        return cell;
    }

    我们先对下面这四个方法设置一下断点,然后观察左边的栈信息。

    首先被调用的是numberOfSectionsInTableView:


    numberOfSectionsInTableView:调用栈信息.png


    我们可以看到从addSubview是如何一步步调用到numberOfSectionsInTableView:的。
    好的,我们下一步看断点断到了tableView:numberOfRowsInSection:上。


    tableView:numberOfRowsInSection:调用栈信息.png


    _rebuildGeometry这个私有方法之前都是一样的,所以我这里并没有全截,可以看到_rebuildGeometry中不仅调用了_updateRowData,还调用了一个_updateContentSize,从这里来获得每个section的行数。
    我们接着往下看,到了tableView:heightForRowAtIndexPath:


    tableView:heightForRowAtIndexPath:调用栈信息.png


    这里通过了一个block回调的方式获取了各个row的高度,并决定了整个section的高度。
    然后我们会发现,以上的几个方法还会再被调用一遍:


    numberOfSectionsInTableView:调用栈信息2.png


    但是栈信息已经不一样了,这次调用时由于tableview调用了layoutSubviews,而reloadData是layoutSubviews里调用的一个方法,因为layoutSubviews也是个公有的方法,所以我们可以用它来触发reloadData。
    断点继续执行,就执行到了tableView: cellForRowAtIndexPath:,我们用它来获取tableview每个row的cell。


    tableView: cellForRowAtIndexPath:调用栈信息.png


    我们会发现tableView: cellForRowAtIndexPath:并不是靠_rebuildGeometry下面的方法来触发,而只是靠layoutSubviews来触发,如果layoutSubviews没有执行成功,那么就可能会遇到我之前遇到过的前几个方法执行而tableView: cellForRowAtIndexPath:不执行的问题。

      • 多了解UIKit的栈信息能够帮我们了解苹果运行的机制和原理,从而帮我们解决一些看起来非常诡异的bug,多看看苹果的私有方法也有助于我们养成良好的编程习惯,我们尽量模仿苹果的代码规范无论是对自己写代码看着舒服,还是对他人来读我们写的代码都一件好事。
  • 相关阅读:
    个推微服务网关架构实践
    NB-IoT 的“前世今生”
    个推基于Consul的配置管理
    个推Node.js 微服务实践:基于容器的一站式命令行工具链
    个推用户画像的实践与应用
    TensorFlow分布式实践
    个数是如何用大数据做行为预测的?
    QCon技术干货:个推基于Docker和Kubernetes的微服务实践
    基于CMS的组件复用实践
    数据可视化:浅谈热力图如何在前端实现
  • 原文地址:https://www.cnblogs.com/jacklandrin/p/6122679.html
Copyright © 2011-2022 走看看