技术分享-TableView性能优化—缓存区

----Make by -LJW 转载请注明出处--- 

  • 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
  • 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
  • 解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
 1 #pragma make 数据源方法
 2 //一共有多少组数据
 3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 4 {
 5     return 1;
 6 }
 7 //第section组有多少row(一组有多少行)
 8 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 9 {
10     return self.heros.count;
11 }
12 
13 #pragma make TableView性能优化
14 //******************************************************************************
15 
16 // cellForRowAtIndexPath 显示的内容(显示的主要内容)
17 /**
18  *  每当有一个cell进入视野范围内,就会调用
19  */
20 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
21 {
22     /*
23     //显示名字和详情的方法
24     //(用户每次拖拽页面都会重新创建新的TableView,多以会有性能问题)
25     //为了解决这个问题,会把多出的一个TableView放到缓存区,用户拖拽的时候用到在拿出来,避免创建多余的,TableView循环使用
26     UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
27     */
28     // static修饰局部变量:可以保     证局部变量只分配一次存储空间(只初始化一次)
29     static NSString *ID = @"hero";
30     // 1.通过一个标识去缓存池中寻找可循环利用的cell
31     // dequeue : 出列 (查找)
32     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
33 
34     // 2.如果没有可循环利用的cell
35     if (cell == nil){
36         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
37 //        NSLog(@"------缓存池找不到cell--%d", indexPath.row);
38     }
39     // 3.给cell设置新的数据
40     //取出模型
41     JWhero *hero = self.heros[indexPath.row];
42     //设置名字
43     cell.textLabel.text = hero.name;
44     //添加第二列详情
45     cell.detailTextLabel.text = hero.intro;
46     //设置头像
47     cell.imageView.image = [UIImage imageNamed:hero.icon];
48 
49     //添加cell右边指示器的样式
50     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
51     //添加cell右边指示器按钮
52 //    cell.accessoryView = [[UISwitch alloc]init];
53 
54     //设置cell默认背景图片 或者 背景颜色(背景view不用设置尺寸,优先级backgroundView > backgroundColor),所以设置默认背景颜色最好用backgroundView
55 //    UIView *bgView = [[UIView alloc]init];
56     UIImageView *bgView = [[UIImageView alloc] init];
57     bgView.image = [UIImage imageNamed:@"buttongreen"];
58 //    bgView.backgroundColor = [UIColor redColor];
59     cell.backgroundView = bgView;
60 
61     //设置cell点击状态背景颜色
62     UIView *selectedbgView = [[UIView alloc]init];
63     selectedbgView.backgroundColor = [UIColor yellowColor];
64     cell.selectedBackgroundView = selectedbgView;
65 
66     return cell;
67 }
原文地址:https://www.cnblogs.com/ljwiOS/p/5183155.html