UITableView的重用机制(缓存机制)
来源:互联网 发布:如何代理淘宝优惠券 编辑:程序博客网 时间:2024/05/22 16:44
UITableView的重用机制
UITableView通过重用单元格来达到节省内存的目的:通过为每个单元格指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,以及当单元格滚出屏幕时,允许恢复单元格以便重用.对于不同种类的单元格使用不同的ID,对于简单的表格,一个标识符就够了.
假如一个TableView中有20个单元格,但是屏幕上最多能显示12个,那么实际上iPhone只是为其分配了12个单元格的内存,没有分配20个,当滚动单元格时,屏幕内显示的单元格重复使用这12个内存,以下代码用于测试内存
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView* table;
int count;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
table=[[UITableViewalloc]initWithFrame:self.view.framestyle:(UITableViewStylePlain)];
table.delegate=self;
table.dataSource=self;
[self.viewaddSubview:table];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return20;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString* cellid=@"cell";
UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellid];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:cellid];
cell.detailTextLabel.text=[NSStringstringWithFormat:@"%d",++count];
}
cell.textLabel.text=[NSStringstringWithFormat:@"%d",indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- UITableView的重用机制(缓存机制)
- UItableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UI -- UITableView的重用机制
- UITableView的禁用重用机制
- UITableView的重用机制原理
- iOS UITableView的重用机制
- HDU - 3251 Being a Hero(最小割)
- Android属性动画完全解析(上),初识属性动画的基本用法
- MySQL忘记密码
- Hello, world!
- UIApplicationDelegate里面最常用的几个函数执行顺序
- UITableView的重用机制(缓存机制)
- CMD——命令提示符
- 实现 strcmp
- 欢迎使用CSDN-markdown编辑器
- Linux常用命令大全
- html中radio的checked与选中
- 《基于BootStrap3的JSP项目实例教程》第5讲
- 二十三种设计模式之创建型模式之抽象工厂
- Java按名字set jdbc参数类