UITableViewCell为什么要复用

UITableView在视图显示位置发生变化之后,会重新创建即将要出现的单元格对象,CocoaTouch框架中,会提供系统的单元格对象复用的机制,避免系统会重复创建对象,开辟新的内存空间
示例代码:

import UIKit

class CustomCell: UITableViewCell {

}

class ViewController: UIViewController, UITableViewDataSource {

    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,x typically from a nib.

        tableView = UITableView(frame: CGRectMake(0, 20, 320, 548), style: .Grouped)
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuse = "reuseCell"
        let cell = UITableViewCell(style: .Default, reuseIdentifier: reuse)

        print(unsafeAddressOf(cell))

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
}

运行输出结果:

0x00007fbb71d5f640
0x00007fbb71d465a0
0x00007fbb71e88de0
0x00007fbb71c166b0
0x00007fbb71c16a80
0x00007fbb71d4c830
0x00007fbb71e91b80
0x00007fbb71d69640
0x00007fbb71e981f0
0x00007fbb71e9c440

0x00007fbb71f16910
0x00007fbb71e9ff80
0x00007fbb71c19e40
0x00007fbb71d6a6f0

前10行是在程序运行起来之后,会创建好的10个显示的单元格,当用户下拉TableView时,会创建新的单元格,从打印地址空行的下面第一个可以看出,新的地址并非已存在的10个单元格地址中的一个。

然后使用单元格复用方法进行测试:
修改UITableViewDataSource中的
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuse = "reuseCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(reuse)
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: reuse)
        }

        print(unsafeAddressOf(cell!))

        return cell!
    }

运行输出结果:

0x00007fa5716237c0
0x00007fa571634bc0
0x00007fa57142d780
0x00007fa571599920
0x00007fa571599cf0
0x00007fa571715e70
0x00007fa571636010
0x00007fa571636f60
0x00007fa571435e40
0x00007fa5716374b0

0x00007fa5716374b0
0x00007fa571435e40
0x00007fa5716374b0
0x00007fa571435e40
0x00007fa5716374b0

从打印输出的结果发现,分行后,tableView需要从数据源(DataSource)方法中获取新的单元格对象时,复用了最开始创建的cell对象的内存空间,也就是同一个对象,这样在实际使用过程中,也就保证了不会重复去开辟新的内存空间,造成性能上和内存上的不必要开销。

使用另外一种复用的方式,在tableView注册单元格复用要使用的类
修改代码:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,x typically from a nib.

        tableView = UITableView(frame: CGRectMake(0, 20, 320, 548), style: .Grouped)
        tableView.dataSource = self
        tableView.registerClass(object_getClass(UITableViewCell()), forCellReuseIdentifier: "reuseCell")
        self.view.addSubview(tableView)
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuse = "reuseCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(reuse, forIndexPath: indexPath)

        print(unsafeAddressOf(cell))

        return cell
    }

打印输出结果:

0x00007fadd162a7d0
0x00007fadd142c780
0x00007fadd150b240
0x00007fadd14211f0
0x00007fadd14371e0
0x00007fadd14375b0
0x00007fadd1437b20
0x00007fadd14385e0
0x00007fadd14390a0
0x00007fadd1439ae0

0x00007fadd1439ae0
0x00007fadd14390a0
0x00007fadd1439ae0
0x00007fadd14385e0
0x00007fadd14390a0
0x00007fadd1439ae0

输出结果与前一种复用的结果一致,但是在代码上更简洁,也是官方推荐的使用方式。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值