
本文為投稿文章,作者:Resory
前言
objcio.cn 上有一篇名為《更輕量的 View Controllers》的文章,其中提到了一個思路:把 UITableView 的 dataSource 與 delegate 從 ViewController 中分離出去。我照著實現了一遍,順手把整個思路記錄下來。
DEMO
建議先下載 demo,再結合下面的分析閱讀,理解起來會更順暢。地址:https://github.com/Resory/RYDatasource
核心邏輯
既然要把 UITableView 的協議方法從 ViewController 中剝離,就需要一個對象來「接住」它們。這裡我們把這個角色稱為 Datasource,你可以把它理解為一個中間人,也就是後續真正實現 UITableView 協議方法的地方。
要讓這個中間人完成協議方法,它就必須掌握三類信息:數據、復用標識以及點擊事件。這些都由 ViewController 傳遞過來,分別對應 serverData、cellIdentifiers 和 configSelectedBlock。
由此可見,只要 ViewController 把這三個基本參數交給中間人,它就能勝任 UITableView 的協議方法實現了。
具體實現
首先在 ViewController.m 中聲明幾個屬性:
(configSelectedBlock 不必設為全局變量,之後在方法裡直接生成即可)
// Viewcontroller.m @property (nonatomic, strong) NSMutableArray *serverData; // 服務器返回的數據 @property (nonatomic, strong) NSMutableArray *cellIdentifiers; // cell 樣式標識 @property (nonatomic, strong) TDatasource *datasource; // 中間人
數據
// Viewcontroller.m - (void)configData { // 服務器返回的數據 _serverData = [[NSMutableArray alloc] init]; // 實體設置 TModelOne *one = [[TModelOne alloc] init]; TModelOne *two = [[TModelOne alloc] init]; one.name = @"奇犽"; one.imageName = @"001.jpg"; two.name = @"拿尼加"; two.imageName = @"002.jgp"; [_serverData addObject:one]; [_serverData addObject:two]; }復用
// Viewcontroller.m - (void)configIdentifier { // cell 復用設置 _cellIdentifiers = [[NSMutableArray alloc] init]; [_cellIdentifiers addObject:NSStringFromClass([TCellOne class])]; [_tableview registerNib:[TCellOne nib] forCellReuseIdentifier:_cellIdentifiers[0]]; }初始化「中間人」
// Viewcontroller.m - (void)configDataSource { // cell 數據 [self configData]; // cell 復用 [self configIdentifier]; // cell 事件 RYCellSelectedBlock cellSelectedBlock = ^(id obj){ // cell 點擊事件 [self cellSelectedWithObj:obj]; }; // 初始化 dataSource _datasource = [[TDatasource alloc] initWithServerData:_serverData andCellIdentifiers:_cellIdentifiers]; _datasource.cellSelectedBlock = cellSelectedBlock; }接著把中間人指定為 UITableView 的協議執行者:
// Viewcontroller.m - (void)configTableView { // 把 _dataSource 設置成 _tableview 的代理,之後所有代理方法都在 _dataSource 中實現 _tableview.delegate = _datasource; _tableview.dataSource = _datasource; _tableview.tableFooterView = [UIView new]; }最後走進中間人內部看看它做了什麼。從下面的代碼不難看出,除了多了一個用來接收數據的初始化方法,其餘全是 UITableView 的協議方法:
// TDatasource.m - (id)initWithServerData:(NSArray *)serverData andCellIdentifiers:(NSArray *)identifiers { self = [super init]; if(self) { self.serverData = serverData; // 數據 self.cellIdentifiers = identifiers; // 復用 } return self; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifiers[0] forIndexPath:indexPath]; // cell 的數據填充(cell 的 category 方法) [cell configCellWithEntity:self.serverData[indexPath.row]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80.0; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // cell 點擊事件 self.cellSelectedBlock(indexPath); }Cell 內部的填充邏輯也一目了然:
#import "TCellOne.h" #import "TModelOne.h" @implementation TCellOne - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)configCellWithEntity:(id)entity { if(entity) { TModelOne *model = entity; self.name.text = model.name; self.avartar.image = [UIImage imageNamed:model.imageName]; } }
總結
把 UITableView 的協議方法從 ViewController 中剝離後,ViewController 確實清爽了不少。但這也帶來了新的不便:多了一個文件,Cell 的點擊事件還得靠 block 回調,若是 Cell 內部子視圖的點擊就更不夠靈活了。凡事有利有弊,最終還是要結合實際項目,選擇最適合自己的開發方式。
如果你有疑問或發現錯誤,歡迎留言交流。
覺得有幫助的話,就點個讚吧,感謝支持~