UITableView的重用机制
来源:互联网 发布:广电网络能吃鸡吗 编辑:程序博客网 时间:2024/06/06 12:47
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indentifier =@"Cell"; UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:indentifier]; if (!cell) {// cell = [[NSBundle mainBundle]loadNibNamed:@"UCFBackMoneyCell" owner:self options:nil][0];//用xib创建的cell cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:indentifier]; } return cell;}
UITableView内部会有两个NSMutableArray:visiableCells内保存当前显示的cells,reusableTableCells保存可重用的cells。
TableView显示之初,reusableTableCells为空,那么[tableView dequeueReusableCellWithIdentifier:indentifier]返回nil。
开始的cell都是通过[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier] 来创建,而且cellForRowAtIndexPath只是调用最大显示cell数的次数。 比如:有100条数据,iPhone一屏最多显示10个cell。
程序最开始显示TableView的情况是:
用[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]创建10次cell,并给cell指定同样的重用标识(当然,可以为不同显示类型的cell指定不同的标识)。并且10个cell全部都加入到visiableCells数组,reusableTableCells为空。
向下拖动tableView,当cell1完全移出屏幕,并且cell1(它也是alloc出来的,原因同上)完全显示出来的时候。cell1加入到visiableCells,cell1移出visiableCells,cell1加入到reusableTableCells。
接着向下拖动tableView,因为reusableTableCells中已经有值,所以当需要显示新的cell,cellForRowAtIndexPath再次被调用的时候,[tableView dequeueReusableCellWithIdentifier:indentifier]返回cell1。 cell1加入到visiableCells,cell1 移出reusableTableCells;cell2移出 visiableCells,cell2加入到reusableTableCells。之后再需要显示的Cell就可以正常重用了。
注意:配置Cell的时候一定要注意,对取出的重用的cell做重新赋值,避免遗留老数据。
- UItableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UITableView的重用机制
- UI -- UITableView的重用机制
- UITableView的禁用重用机制
- UITableView的重用机制原理
- iOS UITableView的重用机制
- UITableView的重用机制原理
- Springboot 整合 Mybatis 的完整 Web 案例
- STL(九)slist单向链表容器
- 求任意两个数之间二进制表示的位数不同的数量之和
- 去探索
- retrofit2简易源码解析
- UITableView的重用机制
- Person Re-Identification CVPR 2017 论文列表
- 浅析ReentLock的使用
- php杂项函数总汇(很实用的未归类函数)
- 2018大疆校招软件岗位笔试题目(求有效工作时间)——未在线验证
- 串口操作经验总结
- contiki之uip-ds6-route头文件
- LintCode:M-不同的二叉查找树个数
- 单例模式:双重锁校验可能存在的问题。