游戏技术文章

iOS-UITableView重用机制 ,图片刷新

时间:2017-4-4 18:10:23  作者:棋牌资源网  来源:棋牌资源网  查看:7955  评论:0
内容摘要:关于讲解UITabel View的使用参照 链接 http://www.bubuko.com/infodetail-974265.html- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtInd...
关于讲解UITabel View的使用

参照 链接 http://www.bubuko.com/infodetail-974265.html

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

identifier

可以看到在创建cell的时候伴随着一个identifier的绑定,这个identifier可以理解为这个cell标识,标识它属于哪个重用队列。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

再来看这句代码,从重用队列中取出一个cell,注意传入的参数identifier,如果把重用队列比作一个房间,那么identifier就好比这个房间的门牌号,标记着要从指定的房间去找人(也就是cell)。另外,入队的时候也会根据cell的identifier放到指定的重用队列中。

可想而知,因为上面那段代码所有的cell都用了相同的identifier,所以只会在一个重用队列中进进出出。假如把代码改成这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

创建cell的时候对每个cell绑定不同的identifier,那么每个cell在入队的时候就会被放到不同的队列中,这样第一遍下拉100个cell都是后每次调用dequeueReusableCellWithIdentifier都无法在对对应重用队列中找到cell,因此要创建100个cell,这就增大了消耗。

注册cell

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];

可以在创建tableView的时候用这个方法注册cell,注册之后的作用是每次从重用列表中取cell 的时候,假如不存在,系统就会自动帮我们创建cell,并绑定标识符identifier。可想而知,注册之后就不需要再写这段代码了:

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

解决列表重复混乱问题

参考文章中,用解决列表重复的3种方法。
我将一下我在开发中实际解决的方案。
我的需求是重用CELL ,但是数据不一样,每个CELL 的长度也是不一样的。不过需要的数据类型是一样的。
重用CELL,
1、清除CELL 的显示的所有数据,
2、赋值CELL新数据,通过代码,重新布局cell。并计算出cell 的高度。
3、在UItabeleView的控制页面,通过字典纪录cell的高度(keys 是我显示数据的唯一ID,value是cell的高度)
4、在UItableView的代理方法中,

(- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,)取出cell的高度进行赋值。

解决列表中刷新图片问题

我需求是CELL 中的图片,之前显示默认的图片,当图片 数据下载成功后,刷新当前cell的页面,显示最新图片数据,

//自定义放大,图片现在下载成功,得到通知调用的方法

-(void)reloadSelectButtonInfo 

{

    NSArray *cells = self.myTabelView.visibleCells; //获取当前显示的cell

    for (int i = 0; i < [cells count]; i++) {

        MyTableViewCell * cell = cells[i];

        [cell UserIsReferenceWithShareItem];//cell 中显示图片的方法

    }

}

标签:iOSUITableView重用机制图片刷新 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved