V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
codeisjobs
V2EX  ›  iDev

iOS 菜鸟,请问创建 UITableViewCell 时,每次创建的 cell 都保存进一个可变数组中,但是复用 cell 时,从数组对应 index 中取出的 cell 的 textLable 的 text 值是错的,什么原因啊

  •  
  •   codeisjobs · 2016-03-23 00:08:03 +08:00 · 2436 次点击
    这是一个创建于 2958 天前的主题,其中的信息可能已经有所发展或是发生改变。

    但是我改成把 cell 的 textLable 的 text 值存进数组,复用时取出来的就是正确的,有什么需要注意的地方吗 代码如下:

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString * timeRing = @"ring";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:timeRing];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:timeRing];
        }else{
            while ([cell.contentView.subviews lastObject]!=nil) {
                [[cell.contentView.subviews lastObject] removeFromSuperview];
            }
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        if (self.cellArray.count<self.ringArray.count) {
      
            self.timeRingItem = self.ringArray[indexPath.row];
            cell.tintColor = [UIColor redColor];
            cell.textLabel.text= self.timeRingItem.ringName;
            [self.cellArray addObject:cell];
        }
        else{
        	cell = self.cellArray[indexPath.row];
        }
        
        return  cell;
    }
    
    9 条回复    2016-03-23 15:49:01 +08:00
    targz
        1
    targz  
       2016-03-23 01:11:11 +08:00   ❤️ 1
    1. cell 为 object ,你存入数组的为此 cell 的指针
    2. 既然已经使用了`tableView`的复用,就不要自己存 cell 。假设页面可以显示 2 个 cell ,你就算有 100 个 cell ,实际上实例化的也大概只有 3 个 cell 。
    也就是说,你的数组里是这样的[cell1, cell2, cell3, cell1, cell2, cell2.....]
    3. 比较合理的方式就是使用 tableView 的 cell 复用。每次拿到 cell 或者实例化 cell 后,将对应的数据赋值即可。
    codeisjobs
        2
    codeisjobs  
    OP
       2016-03-23 01:16:31 +08:00 via iPhone
    @targz 感谢回答,懂了,原来是指针的问题,看来对指针还是不熟啊我
    alexzuo
        3
    alexzuo  
       2016-03-23 08:16:12 +08:00
    @codeisjobs 在公交上 车晃的比较厉害 没仔细看代码 感觉上不是你理解的指针问题 更像是一楼第二条所说的 cell 复用问题
    targz
        4
    targz  
       2016-03-23 08:24:05 +08:00 via iPhone
    @alexzuo 其实他是没明白为什么复用的 cell 存储起来是不对的......
    codeisjobs
        5
    codeisjobs  
    OP
       2016-03-23 08:37:23 +08:00 via iPhone
    @targz 对的,我没考虑到复用的 cell 的指针还是原来的那些,存到数组里结果指针指向的是复用前的对象,所以才会出现存字符串就正确。哎呀,感谢回答,解决了我的一个知识点 bug
    targz
        6
    targz  
       2016-03-23 08:45:15 +08:00 via iPhone
    @codeisjobs 其实很简单,数据是值类型的,那就是值。对象类型的,就是指针。声明的时候加不加星号就能判断出来。
    targz
        7
    targz  
       2016-03-23 08:48:21 +08:00 via iPhone
    @codeisjobs 或者说,实现了 copy 协议的对象,你存一个 copy ,就是新的一个对象了。
    Royuu
        8
    Royuu  
       2016-03-23 11:10:52 +08:00 via iPhone
    @targz 透彻!
    artkernelzyc
        9
    artkernelzyc  
       2016-03-23 15:49:01 +08:00
    @targz cell 复用有多少个,是根据 cell 高度来计算的,和 uitableview 的高度来计算得出的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5199 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:39 · PVG 17:39 · LAX 02:39 · JFK 05:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.