UITableView性能优化
方法的优化
在每个子View中,都会返回,而IOS机制是每次到页面时才会显示,跟ListView是一样的,而IOS的机制是将每个子View存放缓存池中,
缓存池会系统会帮我们创建,系统全自动,缓存池是UITableView内部自带,而因为有不同的子View,因此每个子View都有一个标识,
这样不同类型的cell就却别开来
1、通过一个标识去缓存池中寻找可循环利用的cell
2、如果缓存池中找不到可循环利用的,创建一个新的cell,并且贴一个标识
3、给cell设置新的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
--------------------------------------------------------------------------------------------------------
最基本的优化
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
/**
* 性能优化
*/
//0、这里设置为局部static,就只会分配一次内存,而不用每次刷新都分配内存
static NSString *ID = @"hero";
//1、通过一个标识去缓存池中寻找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2、如果缓存池中找不到可循环利用的,创建一个新的cell,并且贴一个标识
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
//3、给cell设置新的数据
FLHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
@end
三个重点:
1、唯一标识如何设置值:我们就采用数据名,数据名作为存放缓存池标识名
2、唯一标识为什么不用宏,因为标识只用于方法内部使用,而非全局的使用,多处使用才使用宏
3、离开屏幕的cell会怎样?会销毁,如果不做优化,内存会不断创建和销毁,有很大的内存问题
UITableView性能优化
最新推荐文章于 2019-06-23 22:40:59 发布