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
SeanChense
V2EX  ›  iDev

UITableViewCell 的子类向所在的 UIViewController 回传数据最佳实践是?

  •  
  •   SeanChense · 2015-12-25 20:06:06 +08:00 · 2580 次点击
    这是一个创建于 3051 天前的主题,其中的信息可能已经有所发展或是发生改变。
    V2exViewController 中有一个 Cell 叫 V2exTableViewCell, 这个 Cell 内含一个输入框、一个被按下会显示 ActionSheet 的 Button 。这样的 Cell 可能有多个。再复杂点, Cell 中还包含几个 pickerview 呢?
    哈哈,这样的 Cell 真是万金油。。。

    至少, V2exViewController 需要得到 Cell 中 textField 的 text 和 ActionSheet select index ,这里就是两个单独的 delegate 。

    1.如果给 V2exTableViewCell 添加一个 protocol 来向 V2exViewController 回传在 cell 中监听到的数据总觉得很麻烦。

    2.如果 V2exTableViewCell 添加一个 configurationHandler:(V2exHandler)block; 这样的方法去监听 , block 需要被 cell retain 起来才能在 Cell 实现的 textField 的 delegate 中去调用 block ,又会有 strong 和 weak 的麻烦。


    所以,最佳实践是?
    14 条回复    2015-12-29 19:32:17 +08:00
    SeanChense
        1
    SeanChense  
    OP
       2015-12-25 20:13:53 +08:00
    @onevcat 邀请喵神~
    loveuqian
        2
    loveuqian  
       2015-12-25 20:19:06 +08:00 via iPhone
    坐等喵神
    lawder
        3
    lawder  
       2015-12-25 21:24:16 +08:00
    坐等喵神
    SeanChense
        4
    SeanChense  
    OP
       2015-12-25 21:25:16 +08:00
    邀请 @wezzard
    onevcat
        5
    onevcat  
       2015-12-25 21:29:54 +08:00   ❤️ 1
    两者都可以啊..前者复古点但是要改起来灵活解耦好一些,后者稍微方便些但是变动起来困难一点...做 app 的话无所谓哪个都行..感觉并没有很麻烦吧..
    wezzard
        6
    wezzard  
       2015-12-25 21:58:50 +08:00   ❤️ 1
    我碰到這種情況一般都是用第一個方法,雖然很麻煩。如果你要用第二個方法,可以試試 RxSwift ,當然,得要用 Swift 。
    zhangchioulin
        7
    zhangchioulin  
       2015-12-25 22:01:24 +08:00
    我觉得用 Delegate 更好,不过你怕麻烦的话用 Block,很便捷
    banxi1988
        8
    banxi1988  
       2015-12-25 22:47:28 +08:00
    说真的我来提供两种其他方法:
    1 )通过 post Notification 来通信
    2) 反向 delegate 在 Cell 中 保存一个对 ViewController 的 unowned 引用。
    然后操作在 Cell 中处理。
    harde1
        9
    harde1  
       2015-12-26 08:42:00 +08:00 via Android
    用我写的庫吧, tableView 的事情一句代码完成, CommontableCollectionView
    Ixizi
        10
    Ixizi  
       2015-12-26 10:40:02 +08:00
    传值方法 很多啊.都可以.
    aaaron7
        11
    aaaron7  
       2015-12-26 11:30:07 +08:00
    如果你的 cell 要复用,就用 delegate 。

    如果很可能不复用,那直接来个 block 就行了。
    free9fw
        12
    free9fw  
       2015-12-26 12:23:47 +08:00
    block
    zsk425
        13
    zsk425  
       2015-12-26 15:51:34 +08:00
    我也一直有这个困扰,权衡之后觉得还是 delegate 适合更多情况,代码也相对美观。
    PrideChung
        14
    PrideChung  
       2015-12-29 19:32:17 +08:00   ❤️ 1
    还有一种方式是 Target-Action 。 cell 只要沿着响应链一路找就能找到 view controller 。苹果的 Advanced Collection View demo 里面就是这么干的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2103 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 233ms · UTC 01:43 · PVG 09:43 · LAX 18:43 · JFK 21:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.