v2panda的技术专题不明觉厉iOSiOS工作系列

不要把ViewController变成处理tableView的&

2015-12-14  本文已影响10396人  TEASON

请支持原创, 如需转载, 请注明出处@TEASON

之前的文章里我有说要写关于UITableView见解的两篇, 算是展开讨论吧, 这是第一篇, 第二篇下集链接在此.

说在前面:

最近有个MVVM模式非常火热, 相信它的出现是为了模块化iOS开发, 其实在我看来,它始终还是MVC模式, 只是一个变种罢了 .(当然有人用到了响应式编程的思路颠覆了常规 , 但我们今天把讨论点集中于代码的设计模式) .


与其专注于说明 MVVM 的来历,不如让我们看一个典型的 iOS 是如何构建的,并从那里了解MVVM

屏幕快照 2015-12-14 下午3.58.00.png

当然, 关于瘦身ViewController有很多方面 . 然而今天我们讲讲从Controller中分离TableView的表示逻辑 . 为什么引言MVVM设计模式, 也是阐述这个主要思想是相通的 . 就是把"逻辑部分"尽量移到Model层, 你可以认为它是一个中间层 , 所谓"逻辑部分"可以是各种delegate,网络请求,缓存,数据库,coredata等等等等 , 而controller正是用来组织串联他们 .使得整个程序走通 .

正文

我们很容易想到把 UITableViewDataSourceUITableViewDelegate 的代码提取出来放到一个单独的类.
但我发现还是有东西可以抽象出来 .
例如cell的生成, cell行高, 点击等等 .这里我还用了block的形式使得函数能够回调 . 如果你对block还不太了解先看这里 .
此外, 如果你也重度使用.xib生成Cell, 那和我封装的类会非常契合 .
记住我默认习惯用.xib前的文件名来定义cell的Identifier. 如果你想把它用于实战, 记得在xib设置cell的Identifier不要设错.


处理类XTTableDataDelegate.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void    (^TableViewCellConfigureBlock)(NSIndexPath *indexPath, id item, XTRootCustomCell *cell) ;
typedef CGFloat (^CellHeightBlock)(NSIndexPath *indexPath, id item) ;
typedef void    (^DidSelectCellBlock)(NSIndexPath *indexPath, id item) ;

@interface XTTableDataDelegate : NSObject <UITableViewDelegate,UITableViewDataSource>
//1
- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
    cellHeightBlock:(CellHeightBlock)aHeightBlock
     didSelectBlock:(DidSelectCellBlock)didselectBlock ;
//2
- (void)handleTableViewDatasourceAndDelegate:(UITableView *)table ;
//3
- (id)itemAtIndexPath:(NSIndexPath *)indexPath ;

@end

注释: //1. 初始化方法: 传数据源, cellIdentifier, 三个block分别对应配置, 行高, 点击 .
//2. 将UITableViewDataSourceUITableViewDelegate设于XTTableDataDelegate
//3. 默认indexPath.row对应每个dataSource .相应返回item

此外, 为了更彻底, 有必要抽象出"根Cell" .但这样不利于扩展cell . 为了避开modelview的耦合. 所以使用category来做类的扩展 .

#import <UIKit/UIKit.h>

@interface UITableViewCell (Extension)

+ (void)registerTable:(UITableView *)table
        nibIdentifier:(NSString *)identifier ;

- (void)configure:(UITableViewCell *)cell
        customObj:(id)obj
        indexPath:(NSIndexPath *)indexPath ;

+ (CGFloat)getCellHeightWithCustomObj:(id)obj
                            indexPath:(NSIndexPath *)indexPath ;

@end```
故`UITableViewCell+Extension`, 通过类的扩展来实现新Cell .
注释: //1 .不解释. 
//2. 根据数据源配置并绘制cell 子类务必重写该方法
//3. 根据数据源计算cell的高度 子类可重写该方法, 若不写为默认值44.0

pragma mark - Public

pragma mark --

pragma mark - Rewrite these func in SubClass !

}


那么新cell类的实现如下: 实现两个新方法

看下结果, 瘦身后的`controller`干净的不像实力派, 只剩下了这一个方法 .呵呵呵呵 .
诸多`.m`文件太过于冗长,我就不贴到博客了, 博客主要是讲思路, 思路是王道 .
当然如果你想深入理解, 可以看源代码, 我传到了`github`,  [点我下载](https://github.com/Akateason/XTTableDatasourceDelegateSeparation) , 喜欢的话去那加个⭐️, 对开源者是莫大的鼓励 .
任何疑问或建议, 欢迎, 我会看你们的留言 .

> 
[介绍MVVM](http://www.objc.io/issue-13/mvvm.html)
[Lighter View Controllers](http://www.objc.io/issue-1/lighter-view-controllers.html)
[Table View Programming Guide](http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html)
[Cocoa Core Competencies: Controller Object](http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ControllerObject.html)
上一篇下一篇

猜你喜欢

热点阅读