iOS 菜鸟,请问创建 UITableViewCell 时,每次创建的 cell 都保存进一个可变数组中,但是复用 cell 时,从数组对应 index 中取出的 cell 的 textLable 的 text 值是错的,什么原因啊 - V2EX
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 2990 次点击
    这是一个创建于 3502 天前的主题,其中的信息可能已经有所发展或是发生改变。

    但是我改成把 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     Solana     5179 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 07:20 PVG 15:20 LAX 00:20 JFK 03:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86