Swift-dequeueReusableCell与dequeueReusableCell:IndexPath区别

UITableView中单元格复用有两种方法,dequeueReusableCell与dequeueReusableCell:indexPath.

open func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

    @available(iOS 6.0, *)
    open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

① 第一种方法最常用,不管UITableView是否注册,都可以执行以下代码:

var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell")
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "CustomTableCell")
        }

② 第二种方法要求UITableView必须注册类或者nib文件:

self.tableView.register(UINib.init(nibName: "CustomTableCell", bundle:.main), forCellReuseIdentifier: "CustomTableCell")
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableCell", for: indexPath)

如果注册过,第一种方法的cell == nil判断则不需要执行~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,315评论 1 14
  • 更新:2018.05.24 整理了一下demo:SwiftDemo 最近比较忙,没什么时间写,断断续续写一点。 U...
    YvanLiu阅读 34,475评论 6 57
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 9,325评论 1 22
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,348评论 30 472
  • 阅读焦虑症,你得了吗? 近几年,一波阅读狂潮乘着互联网的东风席卷了祖国大江南北,一批批一茬茬爱阅读的年轻人如雨后春...
    彧荻阅读 6,967评论 13 21