请问 tableview(_:cellForRowAtIndexPath) 的实现逻辑是怎样的? - 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
fyl00
V2EX    iDev

请问 tableview(_:cellForRowAtIndexPath) 的实现逻辑是怎样的?

  •  
  •   fyl00 2015-10-16 18:53:30 +08:00 3045 次点击
    这是一个创建于 3725 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 tableview(_:cellForRowAtIndexPath) 方法在 tableView 的某 cell 中 addSubView 之后,在其他个别 cell 里也出现了这个被添加的 view ,虽然通过判断这个 view 是不是存在来删除它,但好奇为什么会出现这种现象。

    本来是猜测 cell 在加载前并不会初始化, view 如果没有通过 indexPath 来修改,那么后面出现的 cell 都会受前面 cell 的影响。但仔细想如果是这样,那为什么不是后来每个 cell 里都会有这个 view 呢?

    代码大致是

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "Cell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomViewCell let viewToAdd = UILable(frame: cell.contentView.frame) ... if (condition) { ... } else { ... cell.contentView.addSubview(viewToAdd) } return cell } 

    所以好奇这个方法的实现逻辑到底是怎么样的……

    17 条回复    2015-10-17 11:30:22 +08:00
    nendors
        1
    nendors  
       2015-10-16 19:10:25 +08:00   1
    都有个 condition 去筛选该 cell 内是否有 viewToAdd 了
    fyl00
        2
    fyl00  
    OP
       2015-10-16 19:25:04 +08:00
    @nendors 那个 condition 是某个数据存在不存在,如果不存在,那么就在这个 cell 里添加 viewToAdd 了。我现在出现的问题是只要添加过一次 viewToAdd ,在其他 Section 的 cell 里就可能会出现这个 view ,也没能发现这个 view 出现的规律
    nendors
        3
    nendors  
       2015-10-16 19:44:51 +08:00
    condition 如果变成存在了 那么接下来的 cell 肯定就有添加 viewToAdd
    你可以把 condition 变成一个[condition]啊 ,然后哪行需要就把对应得 condition 变存在咯
    xi_lin
        4
    xi_lin  
       2015-10-16 20:06:23 +08:00 via iPhone   1
    @fyl00 你 reuse cell 了啊,在 prepareForReuse 里检测删除吧
    fyl00
        5
    fyl00  
    OP
       2015-10-16 20:08:31 +08:00
    @nendors 能问下为什么存在了之后,就添加 viewToAdd 了? addSubview 在 else 语句里不是表示 condition 是 false 的时候才会执行么?
    CDuXZMAPgHp1q9ew
        6
    CDuXZMAPgHp1q9ew  
       2015-10-16 20:08:42 +08:00   1
    cell 是重用的啊亲
    dequeueReusableCellWithIdentifier 这里面几个单词的意思不要忽视啊
    fyl00
        7
    fyl00  
    OP
       2015-10-16 20:10:05 +08:00
    @xi_lin 了解了,多谢~
    fyl00
        8
    fyl00  
    OP
       2015-10-16 20:10:55 +08:00
    @wujichao 啊,我以为 reuse 只是会增加 cell ,现在了解了,多谢~
    Elethom
        9
    Elethom  
       2015-10-16 20:11:07 +08:00
    一堆人半天一用的噗哈哈哈。基起吧太勉自己了。
    fyl00
        10
    fyl00  
    OP
       2015-10-16 20:19:52 +08:00
    @Elethom 所以不是 reuse 的问题么?
    Elethom
        11
    Elethom  
       2015-10-16 20:26:54 +08:00
    @fyl00
    你知道「 reuse 」是什意思?
    rim99
        13
    rim99  
       2015-10-16 22:12:30 +08:00
    回复怎么用 Markdown ?
    CDuXZMAPgHp1q9ew
        14
    CDuXZMAPgHp1q9ew  
       2015-10-16 23:08:32 +08:00
    @Elethom 有事说事 上来就嘲笑 你还是 V2EX 的名人的 就这操行
    ldehai
        15
    ldehai  
       2015-10-16 23:20:16 +08:00 via iPad
    复用是根据 identify 来的,可以设置多个 cellid ,一个 section 对应一个,这样就不会全混在一起了。
    Elethom
        16
    Elethom  
       2015-10-16 23:20:20 +08:00 via iPhone
    @wujichao
    我了「基起吧」。不理解 reuse 制只看象有什用。
    0xfan
        17
    0xfan  
       2015-10-17 11:30:22 +08:00
    我前几天在实现 Custom Collection View 时也遇到了这个疑惑。初学 iOS 开发, Apple 官方的文档是一定要认真读的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2568 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 06:30 PVG 14:30 LAX 22:30 JFK 01:30
    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