您的位置:首页 > 产品设计 > UI/UE

UITableViewCell为什么要复用

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

示例代码:

[code]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
    }
}


运行输出结果:

[code]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

[code]    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!
    }


运行输出结果:

[code]0x00007fa5716237c0
0x00007fa571634bc0
0x00007fa57142d780
0x00007fa571599920
0x00007fa571599cf0
0x00007fa571715e70
0x00007fa571636010
0x00007fa571636f60
0x00007fa571435e40
0x00007fa5716374b0

0x00007fa5716374b0
0x00007fa571435e40
0x00007fa5716374b0
0x00007fa571435e40
0x00007fa5716374b0


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

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

修改代码:

[code]    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
    }


打印输出结果:

[code]0x00007fadd162a7d0
0x00007fadd142c780
0x00007fadd150b240
0x00007fadd14211f0
0x00007fadd14371e0
0x00007fadd14375b0
0x00007fadd1437b20
0x00007fadd14385e0
0x00007fadd14390a0
0x00007fadd1439ae0

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


输出结果与前一种复用的结果一致,但是在代码上更简洁,也是官方推荐的使用方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: