tableView- 性能优化,cell的循环利用三种方式
2015-08-10 21:34
459 查看
tableView性能优化 - cell的循环利用方式1
/** * 什么时候调用:每当有一个cell进入视野范围内就会调用 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 0.重用标识 // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存 static NSString *ID = @"cell"; // 1.先根据cell的标识去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果cell为nil(缓存池找不到对应的cell) if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 3.覆盖数据 cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row]; return cell; }
tableView性能优化 - cell的循环利用方式2
定义一个全局变量// 定义重用标识 NSString *ID = @"cell";
注册某个标识对应的cell类型
// 在这个方法中注册cell - (void)viewDidLoad { [super viewDidLoad]; // 注册某个标识对应的cell类型 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID]; }
在数据源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.去缓存池中查找cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.覆盖数据 cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row]; return cell; }
tableView性能优化 - cell的循环利用方式3
在storyboard中设置UITableView的Dynamic Prototypes Cell设置cell的重用标识
在代码中利用重用标识获取cell
// 0.重用标识 // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存 static NSString *ID = @"cell"; // 1.先根据cell的标识去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.覆盖数据 cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row]; return cell;
错误将UIViewController当做UITableViewController来用
相关文章推荐
- hdu5355 Cake
- excel的几个好用快捷键
- Principles of good RESTful API Design 好的 RESTful API 设计
- poj 2993 Emag eht htiw Em Pleh
- 解决Unsupported major.minor version 51.0错误
- LeetCode之Same Tree
- hdu 2602 - Bone Collector(01背包)解题报告
- 定义自己的Transform Filter
- 粒子滤波(PF:Particle Filter)
- machine learning in coding(python):拼接原始数据;生成高次特征
- [MACOS] Mac上的抓包工具Charles
- vijos1308 埃及分数(迭代加深搜索)
- 设计模式讲解
- 在myeclipse中写mysql语句出现的一个报错
- hadoop配置
- poj 1068 Parencodings
- APP_ABI现有类别
- bitmap
- [HTTP那些事]超大JSON文本
- 2015/8/10/数据库基本操作/单例