码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS开发】关于UITableView的cell循环使用

时间:2015-03-27 22:19:52      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

如果没有让cell循环使用的话。每次展示数据就会有一个cell重新创建,并且如果展示过的数据再次展示还是会重新创建一cell,这样就会造成内存的浪费。

技术分享

解决方法:就是让展示过的cell放入cel池中,每次需要用的时候就拿出来,换一下内容展示出来,滑动到屏幕以外就把这个cell再次放入cell池中等待下一个展示数据用。所以屏幕显示几个cell,这是cell一共需要创建这个多个+1。

比如上图中:屏幕能完整显示11个cell,那么就需要创建12的cell。

如果上上滑动,暴走萝莉出了屏幕,那么显示暴走萝莉这个cell就没用,被存入cell池中,那么显示影流之主下面那个英雄的cell不会被创建,而是拿出上个cell池中得cell更改一下数据显示出来就行了,如此循环下去。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"hero";
    //从池中取出数据
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil) {
        //创建cell,没用的时候就放入ID池中
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    Hero *hero = self.heros[indexPath.row];
    
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    
    return cell;
}


【iOS开发】关于UITableView的cell循环使用

标签:

原文地址:http://blog.csdn.net/ttf1993/article/details/44682141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!