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 · Dec 25, 2015 · 3495 views
    This topic created in 3795 days ago, the information mentioned may be changed or developed.
    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 replies    2015-12-29 19:32:17 +08:00
    SeanChense
        1
    SeanChense  
    OP
       Dec 25, 2015
    @onevcat 邀请喵神~
    loveuqian
        2
    loveuqian  
       Dec 25, 2015 via iPhone
    坐等喵神
    lawder
        3
    lawder  
       Dec 25, 2015
    坐等喵神
    SeanChense
        4
    SeanChense  
    OP
       Dec 25, 2015
    邀请 @wezzard
    onevcat
        5
    onevcat  
       Dec 25, 2015   ❤️ 1
    两者都可以啊..前者复古点但是要改起来灵活解耦好一些,后者稍微方便些但是变动起来困难一点...做 app 的话无所谓哪个都行..感觉并没有很麻烦吧..
    wezzard
        6
    wezzard  
       Dec 25, 2015   ❤️ 1
    我碰到這種情況一般都是用第一個方法,雖然很麻煩。如果你要用第二個方法,可以試試 RxSwift ,當然,得要用 Swift 。
    zhangchioulin
        7
    zhangchioulin  
       Dec 25, 2015
    我觉得用 Delegate 更好,不过你怕麻烦的话用 Block,很便捷
    banxi1988
        8
    banxi1988  
       Dec 25, 2015
    说真的我来提供两种其他方法:
    1 )通过 post Notification 来通信
    2) 反向 delegate 在 Cell 中 保存一个对 ViewController 的 unowned 引用。
    然后操作在 Cell 中处理。
    harde1
        9
    harde1  
       Dec 26, 2015 via Android
    用我写的庫吧, tableView 的事情一句代码完成, CommontableCollectionView
    Ixizi
        10
    Ixizi  
       Dec 26, 2015
    传值方法 很多啊.都可以.
    aaaron7
        11
    aaaron7  
       Dec 26, 2015
    如果你的 cell 要复用,就用 delegate 。

    如果很可能不复用,那直接来个 block 就行了。
    free9fw
        12
    free9fw  
       Dec 26, 2015
    block
    zsk425
        13
    zsk425  
       Dec 26, 2015
    我也一直有这个困扰,权衡之后觉得还是 delegate 适合更多情况,代码也相对美观。
    PrideChung
        14
    PrideChung  
       Dec 29, 2015   ❤️ 1
    还有一种方式是 Target-Action 。 cell 只要沿着响应链一路找就能找到 view controller 。苹果的 Advanced Collection View demo 里面就是这么干的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   907 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    ♥ Do have faith in what you're doing.