代码改变世界

UITableVIew的性能优化-重用原理

2015-07-14 22:40  一树一菩提  阅读(149)  评论(0编辑  收藏  举报

UITableVIew的性能优化:使用方法创建cell时,先优先从缓存池中找cell,找不到再创建新的cell,并且要绑定Identifer标示。

代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//    定义一个静态的标识(只会初始化一次,内存分配一次)

    static NSString *ID = @"c1";

//    优先从缓存池中去找cell

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];

//    缓存池中找不到就创建一个新的cell,一定要绑定标示

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    }