UITableView的重用机制原理
来源:互联网 发布:酒泉百度快照优化 编辑:程序博客网 时间:2024/06/06 20:31
转自:http://www.cnblogs.com/hellocby/archive/2012/05/23/2514469.html
UITableView通过重用单元格来达到节省内存的目的:通过为每个单元格指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,以及当单元格滚出屏幕时,允许恢复单元格以便重用.对于不同种类的单元格使用不同的ID,对于简单的表格,一个标识符就够了.
假如一个TableView中有10个单元格,但是屏幕上最多能显示4个,那么实际上iPhone只是为其分配了4个单元格的内存,没有分配10个,当滚动单元格时,屏幕内显示的单元格重复使用这4个内存,以下代码用于测试内存的使用:
1 - (UITableViewCell *)tableView:(UITableView *)tableView 2 cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 UITableViewCellStyle style = UITableViewCellStyleSubtitle; 5 static NSString *cellID = @"cell"; 6 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 7 if (cell == nil) 8 { 9 cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"cell"] autorelease];10 cell.detailTextLabel.text = [NSString stringWithFormat:@"Cell %d",++count]; //当分配内存时标记11 }12 cell.textLabel.text = [NSString stringWithFormat:@"Cell %d",[indexPath row] + 1]; //当新显示一个Cell时标记13 return cell;14 }
0 0
- UITableView的重用机制原理
- UITableView的重用机制原理
- UItableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UI -- UITableView的重用机制
- UITableView的禁用重用机制
- iOS UITableView的重用机制
- linux下文件数、目录数、文件名长度的各种限制
- Flume-ng ThriftSource原理分析
- IOS中定时器NSTimer的开启与关闭
- Android联系人数据库
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
- UITableView的重用机制原理
- sangsung ussd issue
- 自动调整linux系统时间和时区与Internet时间同步 2011-11-08 09:07:04
- OOM log
- 瞬时响应:网站的高性能架构
- 查看stored procedure的执行计划
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”
- 详解AJAX工作原理以及实例讲解(通俗易懂)
- 在endnote 中添加或更新目的杂志的参考文献格式