UITableViewCell为什么要复用
2016-02-24 17:10
525 查看
UITableView在视图显示位置发生变化之后,会重新创建即将要出现的单元格对象,CocoaTouch框架中,会提供系统的单元格对象复用的机制,避免系统会重复创建对象,开辟新的内存空间
示例代码:
运行输出结果:
前10行是在程序运行起来之后,会创建好的10个显示的单元格,当用户下拉TableView时,会创建新的单元格,从打印地址空行的下面第一个可以看出,新的地址并非已存在的10个单元格地址中的一个。
然后使用单元格复用方法进行测试:
修改UITableViewDataSource中的
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
运行输出结果:
从打印输出的结果发现,分行后,tableView需要从数据源(DataSource)方法中获取新的单元格对象时,复用了最开始创建的cell对象的内存空间,也就是同一个对象,这样在实际使用过程中,也就保证了不会重复去开辟新的内存空间,造成性能上和内存上的不必要开销。
使用另外一种复用的方式,在tableView注册单元格复用要使用的类
修改代码:
打印输出结果:
输出结果与前一种复用的结果一致,但是在代码上更简洁,也是官方推荐的使用方式。
示例代码:
[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
输出结果与前一种复用的结果一致,但是在代码上更简洁,也是官方推荐的使用方式。
相关文章推荐
- while(true)
- 【转载】Hadoop可视化分析利器之Hue
- UITableView小技巧--实现cell向左滑动删除,编辑等功能
- 第一章 遇见HADOOP 第五节 写其它系统比较(hadoop:the definitive guide)
- NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them
- [Sequence Alignment Methods] Cross-Recurrent Plot (CRP)
- 监听UITextField键盘删除按钮事件
- arduino IO口
- iOS uitextField限制输入小数点的位数
- ios UIBezierPath贝塞尔曲线的常用方法与参数说明
- 一个简单的Web UI自动化测试框架Java实现
- UIPickerView的使用(一)
- UiAutoMator一些常用的方法
- 老李分享:接电话扩展之uiautomator
- 老李分享:接电话之uiautomator
- Effective Android UI - MVP
- Your build settings specify a provisioning profile with the UUID “”, however...
- Yum中实现与apt-get install build-essential功能类似的命令
- 给UIColloectionViewCell设置点击效果,解决点击效果错位的问题
- Android Volley完全解析(三),定制自己的Request