UITableViewCell 的子类向所在的 UIViewController 回传数据最佳实践是? - 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
SeanChense
V2EX    iDev

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

  •  
  •   SeanChense 2015-12-25 20:06:06 +08:00 3117 次点击
    这是一个创建于 3582 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     876 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 20:59 PVG 04:59 LAX 13:59 JFK 16:59
    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