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

01-UI基础-04-02-UITableView补充

2016-01-13 17:24 405 查看
##利用缓存池优化列表显示

为所有能现实在用户面前的cell分配内存地址

当一个cell移除用户视野,对应的下一个出现的cell会利用该cell的内存地址

下面是代码部分

/**
*  每一行显示怎么样的内容(call)
*
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.通过一个标示去缓存池中寻找可循环利用的cell
// dequeue:出列(查找)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"A"];

// 2.如果没有可循环利用的cell,就创建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"A"];
}
// 3.给cell设置新的数据
YSHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];

// 设置cell指示器类型
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 设置cell指示器view
cell.accessoryView = [[UISwitch alloc] init];

// 设置背景色(不用设置View宽高)
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bgView;
// 设置选中的单选框背景
UIView *selectView = [[UIView alloc] init];
selectView.backgroundColor = [UIColor brownColor];
cell.selectedBackgroundView = selectView;
NSLog(@"%d-%@-%p",indexPath.row,hero.name,cell);
return cell;
}

实际内存地址使用情况如下。上下移动列表,其内存地址总是保持着11个不同的值,且互相切换。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios ui