您的位置:首页 > 产品设计 > UI/UE

2015年7月10日 UITableView 2

2015-07-10 22:13 465 查看
UITableView

UITableViewCell  的性能优化

原来的代码   这样的话 每当视图 中出现一条table 就会重新创建一个cell  会造成资源的浪费

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// indexPath.row 行
// indexPath.section 组
AutomobileGroups* a1 = self.automobileGroups[indexPath.section];

UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = a1.automobiles[indexPath.row];
return cell;

}

优化三部曲:

1.通过一个标示去缓存池中寻找可循环的cell
2.如果缓存池找不到可循环利用的cell:创建一个新的cell,给cell贴一个标示
3.给cell设置新的数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* I
97b0
D = @"car";

//1.寻找缓存池中 是否有cell
UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

//2.如果没有的话 创建一个
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

//3.重置信息
carGroup* cg = self.groups[indexPath.section];
car* c = cg.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",c.icon]];
cell.textLabel.text = [NSString stringWithFormat:@"%@",c.name];

return  cell;
}

其中 static 也是很重要的优化 因为每次调用这个方法时都初始化一个id 也会消耗内存

如果有static 的话 只会在第一次 进行初始化

cell的 属性



显示 右边的索引

- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [self.groups valueForKeyPath:@"title"];//kvc
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview