---
title: "总结 ﹣ UITableView (性能优化①)"
author: "cwlong"
date: "2015-07-28T22:08:09Z"
tags: []
categories: ["编程语言"]
slug: "uitableview"
draft: false
lastmod: "2015-07-30T21:14:06Z"
id: 2230991
url: "https://cwlong.iteye.com/blog/2230991"
---

<span style="color:red">注 : 文章不断更新,转载文章请加上作者 </span>

[ 总结 ﹣ UITableView ](http://cwlong.iteye.com/admin/blogs/2230371) 中,说了如何遵守协议调用方法,使UITableView展示数据: 如下面代码所示


```c

//这种调用方法,固然可以从数据模型中获取到数据,并且展示出来,
//每当一个cell进入视野范围内,就会调用一次方法(打印一下就可以测试)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[uitableviewcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    Apple *apple = self.apple[indexpath.row];
    
    cell.textLabel.text =apple.name;
    
    cell.detailTextLabel.text = apple.intro;
    
    cell.imageView.image = [uiimage imagenamed:apple.icon];
    
    return cell;
}


```


<span style="color:red">不好的地方:(对性能的影响)</span>
1 . 一开始就创建了可视范围的多个对象
2 . 每调用一次都分配<span style="color:red">新的</span>存储空间,去创建UITableViewCell对象 , 也就是说, 假如滚动很快,快速来回滚动的时候,内存会忽然飙升得很高.(因为不断 )


<u>性能优化 : </u>

性能优化的思路 :  

假如屏幕只能显示<span style="color:red">八块UITableViewCell </span>, 引入一个**<span style="color:red">缓存池机制 </span>**,当继续滚动到下一个的时候 , 这个时候创建<span style="color:red">第九块 UITableViewCell</span> , 然后<span style="color:red">第一块UITableViewCell</span>完全消失的时候,将它放入**<span style="color:red">缓存池 </span>**中,等到<span style="color:red">第十块UITableViewCell</span>需要再创建的时候,从**<span style="color:red">缓存池 </span>**中拿出<span style="color:red">第一块UITableViewCell</span>  , 周而复始.

<u>***<span style="color:red">所以. 屏幕假如只显示8块Cell  ,  整个过程至销毁只要创建9块Cell对象即可.整个过程怎样滚动也不需要在多创建UITableViewCell</span>***</u>

<span style="color:blue">注意点:</span>
<span style="color:blue"> 当开发程序复杂的时候,</span>**<span style="color:red">缓存池 </span>**<span style="color:blue">里面有各种各样的东西,很多的cell等,所以怎样知道区分cell对应的UITableView呢?</span>

解决办法: 每次创建UITableViewCell , 都给它绑定一种类型(一种标示等) , 然后从**<span style="color:red">缓存池 </span>**<span style="color:blue"></span>中找的时候,根据类型(标示等)去找到对应的
cell用来重用

步骤:
 1. 通过一个标识,去缓存池中寻找可循环利用的cell
 2. 如果缓存池找不到, 可循环利用的cell: 创建一个新的cell , 给cell贴个标识
 3. 给cell设置新的数据.

代码如下 : 


```c


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //0. static修饰局部变量: 可以保证局部变量只分配以此存储空间(只初始化一次)
    static NSString *ID = @"Apple";    

    //1. 通过一个标识,去缓存池中寻找可循环利用的cell
    UITableViewCell *cell = [tableview dequeuereusablecellwithidentifier:id];
    
    //2. 如果缓存池找不到, 可循环利用的cell: 创建一个新的cell , 给cell贴个标识
    if (cell == nil) {
        cell = [[uitableviewcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    //3 经过前两步后肯定又Cell了.给cell设置新的数据.
    Apple *apple= self.apple[indexpath.row];
    
    cell.textLabel.text =apple.name;
    
    cell.detailTextLabel.text = apple.intro;
    
    cell.imageView.image = [uiimage imagenamed:apple.icon];
    
    
    NSLog(@"%p  -  %@   -   %ld ",cell,apple.name,indexPath.row);
    
    return cell;
}


```