UITableview优化

说明:

  • 开始只创建一部分可见的cell(已由Xcode实现)
  • 在滚动的时候,先去缓存池中查找可用的cell,如果有,则取出使用

优化的两种方法

  • 方法1:在创建cell时先去缓存池找是否有可用的cell
    NSString *ID=@"test";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"test%zd",indexPath.row];
  • 方法2:注册cell
    //在viewDidLoad方法注册cell,注册一次即可
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

    //注册之后效果同方法1
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.textLabel.text=[NSString stringWithFormat:@"test%zd",indexPath.row];
posted @ 2016-04-04 19:11  爱上咖啡的唐  阅读(102)  评论(0编辑  收藏  举报