美文网首页程序员
(Swift-4.0/OC-iOS11)tableview中的e

(Swift-4.0/OC-iOS11)tableview中的e

作者: atme | 来源:发表于2018-06-07 18:49 被阅读97次

    什么是estimatedRowHeight? estimatedRowHeight对tableview的复用池机制有哪些影响?怎么去解决?

    什么是estimatedRowHeight?

    show Quick help一下,看看苹果怎么说 estimatedRowHeight属性说明
        本着翻译只翻译重点的原则,大致的意思: estimatedRowHeight是一个预估高度,在iOS11之前是为0,在iOS11之
    后,这个值默认为44。在swift4.0,默认跟ios11保持一致,问题出现了,这个默认值会有什
    么影响?
    

    对tableview的复用池机制的影响:

    首先苹果为什么要改变 estimatedRowHeight的默认值呢?

    我们知道tableView是继承于ScrollView的,一个scrollView能滑动,需要设置contentSize,
    那么tableView的contentSize怎么来呢?iOS11之前(swift4.0之前),会调用tableView每一个cell的heightForRowAtIndexPath
    来算出整个高度,从而相加得出contentSize来,这一个步骤挺耗性能!
    
    所以iOS11,默认打开了estimatedRowHeight估算高度功能,当tableView创建完成后,
    contentSize为estimatedRowHeight(默认值为44)*cell的数量,不需要遍历每一个cell的heightForRowAtIndexPath来计算了。
    

    所以说苹果爸爸这个改变还是很OK的,那么设置44和0会有什么影响?下面我们来看一段代码

    /*创建一个tableview,注册MasterTableViewCell, estimatedRowHeight设置为44,或者
    不写,数据源filteredPokemons.count是17,屏幕最多能显示5个cell*/
    
    lazy var myTableView: UITableView = { [unowned self] in
        let myTableView =  UITableView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64), style: .plain)
         myTableView.tableFooterView = UIView.init()
         myTableView.dataSource = self
         myTableView.delegate = self
         myTableView.estimatedRowHeight = 44(或者0);
         myTableView.register(MasterTableViewCell.classForCoder(), forCellReuseIdentifier: MasterCell)
         return myTableView
    }()
    
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
    
    // MARK: - UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredPokemons.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: MasterCell, for: indexPath) as? MasterTableViewCell
        let pokemon = filteredPokemons[(indexPath as NSIndexPath).row]
        cell?.model = pokemon
        return cell!
    }
    
    cell内部实现:
     override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        let initCount =  LibraryAPI.sharedInstance.getInitCount()
        print("初始化走了多少次 \(initCount)")
        self.addSubview(self.imgView)
        self.addSubview(self.titleLabel)
        self.contentView.addSubview(self.subTitleLabel)
    }
    

    运行一下estimatedRowHeight = 44;情况

    初始化走了多少次 1
    初始化走了多少次 2
    初始化走了多少次 3
    初始化走了多少次 4
    初始化走了多少次 5
    初始化走了多少次 6
    初始化走了多少次 7
    初始化走了多少次 8
    初始化走了多少次 9
    初始化走了多少次 10
    初始化走了多少次 11
    初始化走了多少次 12
    初始化走了多少次 13
    初始化走了多少次 14
    初始化走了多少次 15
    初始化走了多少次 16
    
    结果整整init了16次,然而屏幕最多显示5个cell,复用池是不是有问题? 
    

    我们再看看estimatedRowHeight = 0;

    lazy var myTableView: UITableView = { [unowned self] in
        
        let myTableView =  UITableView(frame: CGRect(x: 0, y: 88, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-88), style: .plain)
         myTableView.tableFooterView = UIView.init()
         myTableView.dataSource = self
         myTableView.delegate = self
         myTableView.estimatedRowHeight = 0;
         myTableView.register(MasterTableViewCell.classForCoder(), forCellReuseIdentifier: MasterCell)
         return myTableView
    }()
    

    看看运行结果:

    初始化走了多少次 1
    初始化走了多少次 2
    初始化走了多少次 3
    初始化走了多少次 4
    初始化走了多少次 5
    
    初始化只走了5次,刚好是屏幕显示的cell的个数.复用池正常
    

    那么estimatedRowHeight的值为什么会造成这样的问题?

    我们假设数据源是17个,cell是17个,高度是140,设置estimatedRowHeight=44,那么会触发
    17次heightForRow,tablev的初始contenSize就是 17*140 = 2380,按理说初始化的个数
    应该是2380/44 = 54个,查看上面的打印日志estimatedRowHeight = 44,为什么17个数据
    ,只初始化了16次?,应该是17个呀?后面经过调试你会发现,实际上会调用16次
    heightForRow的方法,这16次,是tableview的初始化高度36(iPhone 7p的高度)-64(导航
    栏的高度) = 672,724/44 = 15.24 ,然后是16次
    

    结论:

    当你的实际高度大于预估高度的时候,会按照预估高度下的cell的数量来计算contentSize,
    当实际高度小于预估高度的时候,会按照实际高度下的cell的数量来计算contentSize。
    
    如果我们要回到iOS11之前的效果,我们可以让estimatedRowHeight=0,关闭这个预估高度
    的效果。
    

    其他解决问题:

    为什么使用MJRefresh在iOS11下要让estimatedRowHeight=0,因为MJRefresh底部的上拉
    刷新是根据contentSize来计算的,当数据更新的时候,得出来的contentSize只是预估的。

    相关文章

      网友评论

        本文标题:(Swift-4.0/OC-iOS11)tableview中的e

        本文链接:https://www.haomeiwen.com/subject/xkggsftx.html