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 · 2656 次点击
    这是一个创建于 3112 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 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 的时候才会执行么?
    wujichao
        6
    wujichao  
       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 ?
    wujichao
        14
    wujichao  
       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   ·   我们的愿景   ·   实用小工具   ·   1064 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:32 · PVG 06:32 · LAX 15:32 · JFK 18:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.