UITableView-04Cell的循环利用
来源:互联网 发布:天津网络推广公司 编辑:程序博客网 时间:2024/06/10 02:35
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的标识去缓存池中查找可循环利用的cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 2.覆盖数据cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];return cell;
下面对之前LOL英雄项目进行优化
- 用方式2进行cell的循环利用
- viewController.m 源文件
//// ViewController.m// LOL英雄-单组数据//// Created by Kavee DJ on 16/5/13.// Copyright © 2016年 Kavee DJ. All rights reserved.//#import "ViewController.h"#import "KDJHero.h"@interface ViewController () <UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (nonatomic, strong) NSArray *heroes;@end@implementation ViewController// 懒加载- (NSArray *)heroes{ if (_heroes == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"heroes.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *heroArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { KDJHero *hero = [KDJHero heroWithDict:dict]; [heroArray addObject:hero]; } _heroes = heroArray; } return _heroes;}// 定义重用标识NSString *ID = @"hero";- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 注册某个标识对应的cell类型 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - <UITableViewDataSource>// 默认就是1组//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//{// return 1;//}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.heroes.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.去缓存池中查找cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.覆盖数据 KDJHero *hero = self.heroes[indexPath.row]; cell.textLabel.text = hero.name; cell.imageView.image = [UIImage imageNamed:hero.icon]; cell.detailTextLabel.text = hero.intro; return cell;}@end
0 0
- UITableView-04Cell的循环利用
- cell的循环利用
- 关于cell的循环利用
- 【iOS开发】关于UITableView的cell循环使用
- UITableView的Cell排序
- UITableView cell的重用
- UITableView cell的重复使用
- UITableView 的 自定义cell
- UITableView的自定义cell
- cell重用(循环利用)
- tableView- 性能优化,cell的循环利用三种方式
- 改变UITableView Cell的字体
- UITableView 删除特定的CELL
- 关于UITableView的Cell复用
- 禁止编辑UITableView的Cell
- UITableView获取cell的indexPath
- UITableView cell 的动态使用
- 关于UITableView的Cell复用
- 2016年武汉科技大学邀请赛网络赛 F题
- QTableWidget使用总结
- db2 常用sql
- <html>img、map、area
- JAVA程序员一定知道的优秀第三方库(2016版)
- UITableView-04Cell的循环利用
- sql的子连接查询
- java网络编程(URL类)
- Json格式
- 安卓中进行基于HTTP协议的网络访问
- ORA-01045: user lacks CREATE SESSION privilege解决办法
- 【32】树的子结构
- 传统事件绑定、DOM2级事件绑定、以及IE事件绑定区别
- poj1363 Rails(栈模拟)