TableView 是iOS app 中最常用的控件甸祭,許多代碼直接或者間接的關聯(lián)到table view任務中,包括提供數(shù)據(jù)缤言、更新tableView欺抗、控制tableView行為等等。下面會提供保持tableView代碼整潔和結(jié)構(gòu)清晰的方法恕沫。
UITableViewController vs. UIViewController
TableViewController的特性
table view controllers可以讀取table view的數(shù)據(jù)监憎、設置tabvleView的編輯模式、反應鍵盤通知等等婶溯。同時Table view controller能夠通過使用UIRefreshControl來支持“下拉刷新”鲸阔。
Child View Controllers
tableViewController也可以作為child view controller添加到其他的viewController中,然后tableViewController會繼續(xù)管理tableView迄委,而parentViewController能管理其他我們關心的東西褐筛。
-(void)addDetailTableView
{
DetailViewController *detail = [DetailViewController new];
[detail setup];
detail.delegate = self;
[self addChildViewController:detail];
[detail setupView];
[self.view addSubview:detail.view];
[detail didMoveToParentViewController:self];
}
如果在使用以上代碼時,需要建立child View controller 和 parent view controller之間的聯(lián)系跑筝。比如死讹,如果用戶選擇了一個tableView里的cell,parentViewController需要知道這件事以便能夠響應點擊時間曲梗。所以最好的方法是table view controller定義一個協(xié)議赞警,同時parent view controller實現(xiàn)這個協(xié)議。
@protocol DetailViewControllerDelegate
-(void)didSelectCell;
@end
@interface ParentViewController () <DetailViewControllerDelegate>
@end
@implementation ParentViewController
//....
-(void)didSelectCell
{
//do something...
}
@end
雖然這樣會導致view controller之間的頻繁交流虏两,但是這樣保證了代碼的低耦合和復用性愧旦。
分散代碼
在處理tableView的時候,會有各種各樣不同的定罢,跨越model層笤虫、controller層、view層的任務祖凫。所以很有必要把這些不同的代碼分散開琼蚯,防止viewController成為處理這些問題的“堆填區(qū)”。盡可能的獨立這些代碼惠况,能夠使代碼的可讀性更好遭庶,擁有更好的可維護性與測試性。
消除ModelObeject和Cell之間的隔閡
在很多情況下稠屠,我們需要提交我們想要在view層展示的數(shù)據(jù)峦睡,同時我們也行維持view層和model層的分離翎苫,所以tableView中的dateSource常常做了超額的工作:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[cell setup];
NSString *text = self.title;
cell.label.text = text;
UIImage *photo = [UIImage imageWithName:text];
cell.photoView.image = photo;
}
這樣dataSorce會變得很雜亂,應該將這些東西分到cell的category中榨了。
@implementation Cell (ConfigText)
-(void)configCellWithTitle:(NSString *)title
{
self.label.text = title;
UIImage *photo = [UIImage imageWithName:title];
cell.photoView.image = photo;
return cell;
}
這樣的話dataSource將會變得十分簡單煎谍。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[cell configCellWithTitle:self.title];
return cell;
}
復用cell
其實還可以更進一步,讓同一個cell變得可以展示多種的modelObject龙屉。首先需要在cell中定義一個協(xié)議呐粘,想要在這個cell中展示的object必須遵守這個協(xié)議。然后可以修改分類中config method來讓object來遵守這個協(xié)議叔扼,這樣cell就能適應不同的數(shù)據(jù)類型事哭。
在cell中處理cell狀態(tài)
如果想要對tableView的行為進行設置,如選中操作后改變高光狀態(tài)等瓜富,可以在tableViewController中使用委托方法:
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.label.shadowColor = [UIColor greenColor];
}
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.label.shadowColor = nil;
}
然而當想要換出這些cell或者想要重新設計的時候鳍咱,仍然需要適應委托方法。cell里面的detail的實現(xiàn)和委托方法中對detail的實現(xiàn)交織在一起与柑,所以應該將這些邏輯移到cell里面:
@implementation Cell
//...
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if(highlighted)
{
self.label.shadowColor = [UIColor greenColor];
}
else
{
self.label.shadowColor = nil;
}
}
@end
一個委托需要知道一個view的不同狀態(tài)谤辜,但它不需要知道怎么去修改view或者有哪些屬性需要設置來使這個view轉(zhuǎn)變狀態(tài),所有的邏輯應該又view來完成价捧,而在外部只是僅僅提供一個API丑念。這樣才是view層和controller層實現(xiàn)代碼之間的有效分離。
處理不同的cell類型
如果在一個tableView中有不同的cell類型结蟋,dataSource將會變得膨脹而難以操作脯倚,在下面的代碼中,有兩個不同的cell類型嵌屎,一個負責展示圖片和標題推正,另一個負責展示星標。為了分離處理不同的cell的代碼宝惰,dataSource方法只是僅僅執(zhí)行不同cell自己的設置方法植榕。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL isStarRank = self.keys[(NSUInteger)indexPath.row];
UITableViewCell *cell;
if(isStarRank)
{
cell = [self setupStarCell];
}
else
{
cell = [self setupDefaultCell];
}
}
-(StarCell *)setupStarCell
{
//do something...
}
-(UITableViewCell *)setupDefaultCell
{
//do something...
}
編輯TableView
TableView提供了方便的編輯功能,能夠刪除和移動cell尼夺。這些事件中尊残,tableView的dataSource通過委托方法獲取通知,因此經(jīng)常在這些委托方法中出現(xiàn)對數(shù)據(jù)的修改淤堵,而修改數(shù)據(jù)很明顯是model層的任務寝衫。model應該提供刪除、排序等的接口拐邪,這樣就能夠通過dataSource的方法來調(diào)用竞端。從而controller扮演了view和model之間的協(xié)調(diào)者,而不需要知道m(xù)odel層的實現(xiàn)細節(jié)庙睡。同時這樣model的邏輯會變得更容易測試事富,因為沒有混雜viewController的任務。
總結(jié)
tableViewController以及其他controller應該扮演model和view的協(xié)調(diào)者和中介者乘陪,而不應該關心屬于view或者model層的任務统台。謹記這點,讓委托和dataSource變得更小和只包含公式化的代碼啡邑。
這不只是減少tableViewController的體積和復雜度贱勃,同時也把域邏輯和界面邏輯放到相關的類中,把實現(xiàn)細節(jié)包裹在簡單的API接口中谤逼,最終提高了代碼的可讀性和代碼的協(xié)調(diào)能力贵扰。
以下文章可以做一個學習參考:
GCD面試要點
block面試要點
Runtime面試要點
RunLoop面試要點
內(nèi)存管理面試要點
MVC、MVVM面試要點
網(wǎng)絡性能優(yōu)化面試要點
網(wǎng)絡編程面試要點
KVC&KVO面試要點
數(shù)據(jù)存儲面試要點
混編技術(shù)面試要點
設計模式面試要點
UI面試要點