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

##利用缓存池优化列表显示缓存

  1. 为全部能现实在用户面前的cell分配内存地址
  2. 当一个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个不一样的值,且互相切换。 code

相关文章
相关标签/搜索