##利用缓存池优化列表显示缓存
下面是代码部分优化
/** * 每一行显示怎么样的内容(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