在App中劝堪,比如側(cè)邊欄秒啦,設(shè)置,個(gè)人界面荐糜,編輯表單葛超,總會(huì)有一些地方用到一些靜態(tài)Cell構(gòu)成的TableView绣张,這些界面的特點(diǎn)是关带,Cell種類繁多宋雏,Model種類繁多,點(diǎn)擊事件處理分散但單調(diào)(頁(yè)面跳轉(zhuǎn)嗦明,編輯textFiled)娶牌,重復(fù)寫tableViewDelegate和datasource的相關(guān)方法馆纳,但不會(huì)用到一些奇怪的datasource和delegate方法鲁驶,而且顯示內(nèi)容一般是固定的,每個(gè)頁(yè)面Cell的數(shù)量不會(huì)很多径荔。像筆者所在的項(xiàng)目里的側(cè)邊欄模塊猖凛,里面有很多tableView绪穆,由于沒(méi)有使用StoryBoard且很久之前寫的,充斥著各種if...else if用model的某個(gè)key值進(jìn)行判斷的邏輯去讓cell去顯示和隱藏某些視圖菠红,已經(jīng)進(jìn)行點(diǎn)擊跳轉(zhuǎn)邏輯试溯,更有甚者遇绞,有if (indexPath == 0)
之類的通過(guò)行數(shù)判斷去處理的邏輯。增加新的cell時(shí)蹄咖,便要在各種地方再加上else if的新判斷澜汤,而做新界面時(shí)舵匾,也沒(méi)有很好的方法進(jìn)行復(fù)用坐梯。筆者結(jié)合之前文章進(jìn)行進(jìn)一步思考,抽象出一個(gè)適合這種特點(diǎn)tableView的管理類烛缔,記錄一下思路供參考馏段。
關(guān)于TableViewDatasource的思考
在之前的文章(iOS通用的DataSource)中,已經(jīng)對(duì)Datasource和SectionObject進(jìn)行了抽象践瓷,datasource本質(zhì)是告訴Cell如何去處理Model的橋梁院喜,之前筆者是通過(guò)維護(hù)一個(gè)字典去一一對(duì)應(yīng)Model的種類和Cell種類的,但后來(lái)發(fā)現(xiàn)晕翠,有時(shí)候Model和Cell的種類并不一定是1對(duì)1的喷舀,比如有些時(shí)候cell只要只要展示一個(gè)title,可能使用者只想寫一個(gè)String去對(duì)應(yīng)淋肾,而不用去建一個(gè)Model,在靜態(tài)不變的Model中樊卓,這種情況更突出拿愧,因?yàn)椴⒉恍枰獜木W(wǎng)絡(luò)解析數(shù)據(jù),不用用反射機(jī)制去進(jìn)行json轉(zhuǎn)model的處理碌尔,有時(shí)候浇辜,一個(gè)NSDictionary甚至一個(gè)NSString或者Bool就可以滿足一個(gè)cell的展示需求券敌,那么對(duì)于這種可能是多Cell對(duì)多Model(String也可以是一種Model)的情況,且每個(gè)TableView的Cell數(shù)量不多(一般設(shè)置頁(yè)不會(huì)超過(guò)20個(gè))柳洋,可以考慮為在字典里每個(gè)model配置對(duì)應(yīng)的Cell類型的鍵值對(duì)待诅。而且因?yàn)镸odel種類不一,我們也需要手動(dòng)Add到SectionObject里熊镣,那么我們可以把這個(gè)字典分在每個(gè)sectionObject里去管理卑雁。在section類里添加以下方法,而model的唯一性可以用hash屬性標(biāo)識(shí)绪囱,默認(rèn)hash屬性就是對(duì)象的內(nèi)存地址测蹲。
- (void)addItem:(id<NSObject>)item cellClass:(Class)cellClass{
if (!item) {
return;
}
[_items addObject:item];
if (!cellClass) {
return;
}
NSString *itemHash = [NSString stringWithFormat:@"%tu",item.hash];
[self.sectionCellDic setObject:cellClass forKey:itemHash];
}
然后再增加外部通過(guò)indexPath讀取對(duì)應(yīng)Cell Class的方法
- (Class)cellClassIndexPath:(NSIndexPath *)indexPath{
return [self cellClassForItem:[self itemForIndexPath:indexPath]];
}
- (Class)cellClassForItem:(id<NSObject>)item{
NSString *itemHash = [NSString stringWithFormat:@"%tu",item.hash];
Class cellClass = [self.sectionCellDic objectForKey:itemHash];
return cellClass;
}
- (id)itemForIndexPath:(NSIndexPath *)indexPath{
if (self.items.count > indexPath.row) {
return self.items[indexPath.row];
}
return nil;
}
接管TableViewDelegate
和很多動(dòng)態(tài)Cell會(huì)有一些奇奇怪怪的Delegate不同,靜態(tài)cell主要就是展示和點(diǎn)擊事件毕箍,一般會(huì)用到以下原生delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)sectionIndex;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)sectionIndex;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sectionIndex;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
所以我們可以讓TableViewManager接管這些協(xié)議的實(shí)現(xiàn)弛房,同時(shí)新建一個(gè)TableViewManagerDelegate為UITableViewDelegate的子協(xié)議,并開放這些協(xié)議給使用Manager的人而柑,也就是可以讓使用者覆寫這些delegate。
比如heightForRowAtIndexPath方法荷逞,就可以在判斷外部是否實(shí)現(xiàn)媒咳,否則則內(nèi)部實(shí)現(xiàn)。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
return [self.delegate tableView:tableView heightForRowAtIndexPath:indexPath];
}
Class cellClass = [self tableView:tableView cellClassForRowAtIndexPath:indexPath];
id item = [self tableView:tableView itemForRowAtIndexPath:indexPath];
return (cellClass == [UITableViewCell class])?44.f:[cellClass tableView:tableView rowHeightForItem:item];
}
同理种远,這些協(xié)議的默認(rèn)實(shí)現(xiàn)可以根據(jù)實(shí)際情況作調(diào)整涩澡,比如對(duì)于didSelectRowAtIndexPath,我們也可以帶上其item再返回給外部坠敷,帶上item的好處妙同,等會(huì)會(huì)講到。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(tableView:didSelectItem:atIndexPath:)]) {
id item = [self tableView:tableView itemForRowAtIndexPath:indexPath];
[self.delegate tableView:tableView didSelectItem:item atIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
Cell的協(xié)議里有啥
之前文章講過(guò)膝迎,我們可以通過(guò)一個(gè)CellProtocol去指定configItem的方法讓cell自己去解析Model粥帚,同時(shí)我們也可以讓這個(gè)協(xié)議豐富一點(diǎn),比如加上CellDelegate的回調(diào)限次,可以讓Cell把信息傳到外部芒涡,這在一些Cell里面的點(diǎn)擊事件,或者開關(guān)(UISwitch)卖漫,表單編輯時(shí)都很有用费尽,所以我們可以加上delegate
@protocol CDZTableViewCellDelegate <NSObject>
@optional
- (void)didTriggleCell:(UITableViewCell*)cell message:(id)message;
@end
@protocol CDZTableViewCell<NSObject>
@required
- (void)configWithItem:(id<NSObject>)item;
+ (CGFloat)tableView:(UITableView *)tableView rowHeightForItem:(id)item;
@optional
- (void)setCDZCellDelegate:(id<CDZTableViewCellDelegate>)objectDelegate;
@end
而這個(gè)Message的執(zhí)行者應(yīng)該是外部調(diào)用者而不是Manager本身,所以對(duì)于Cell和外部來(lái)說(shuō)羊始,它雖然是個(gè)id但實(shí)際類型是外部和Cell內(nèi)部統(tǒng)一知道的旱幼,比如是個(gè)Bool值表示開關(guān)狀態(tài),一個(gè)NSDictionary里面包含信息突委,或者是一個(gè)NSString柏卤。而Manager只要做一個(gè)中轉(zhuǎn)就好了叹誉。這里我在Manager里把Cell轉(zhuǎn)換成了對(duì)應(yīng)的Item及IndexPath傳給外部,因?yàn)镃ell是可復(fù)用的闷旧,實(shí)際關(guān)心的應(yīng)該是Model长豁。
- (void)didTriggleCell:(UITableViewCell *)cell message:(id)message{
if ([self.delegate respondsToSelector:@selector(receiveCellMessage:atIndexPath:item:)]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
id item = [self tableView:self.tableView itemForRowAtIndexPath:indexPath];
[self.delegate receiveCellMessage:message atIndexPath:indexPath item:item];
}
}
初始化方法
- (instancetype)initWithTableView:(UITableView *)tableView delegate:(id<CDZTableViewManagerDelegate>)objectDelegate{
self = [super init];
if (self){
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_sections = [NSMutableArray array];
_delegate = objectDelegate;
self.tableView = tableView;
}
return self;
}
最后外界只需要初始化一個(gè)manager,即可不用寫Datasource和Delegate了忙灼,因?yàn)槎急籑anager接管了匠襟。
item的一些小技巧
對(duì)應(yīng)一些初展示類或者開關(guān)操作的item,我們可以用簡(jiǎn)單的Bool该园,NSString酸舍,NSDictionry作為Model,而有點(diǎn)擊事件的Model里初,我們可以在model類里增加一個(gè)tapBlock啃勉,這樣我們可以在配置item時(shí)順便配置其點(diǎn)擊事件。因?yàn)殪o態(tài)cell中的點(diǎn)擊事件往往各不相同双妨,所以配置在tapBlock可以讓代碼更加統(tǒng)一淮阐。
TestItem *itemD = [[TestItem alloc]init];
itemD.title = @"itemD";
itemD.tapBlock = ^(TestItem *item) {
NSLog(@"%@ tap",item.title);
};
[firstSection addItem:itemD cellClass:[TestBCell class]];
這樣不僅好找,一次性把相關(guān)的操作配置完成刁品,而且在執(zhí)行點(diǎn)擊事件時(shí)泣特,不用再關(guān)心具體的點(diǎn)擊事件,進(jìn)行item的識(shí)別分類判斷挑随。
- (void)tableView:(UITableView *)tableView didSelectItem:(id)item atIndexPath:(NSIndexPath *)indexPath{
if ([item isMemberOfClass:[TestItem class]]) {
TestItem *cellItem = (TestItem *)item;
if (cellItem.tapBlock) {
cellItem.tapBlock(cellItem);
}
}
}
最后
通過(guò)這些的Manager状您,就可以隨心所欲搭配model和cell,并把配置寫在一起兜挨,方便修改膏孟,查找,同時(shí)也不用重復(fù)寫Delegate和Datasource拌汇,而對(duì)老的Cell和Model也有很好的兼容柒桑。底下是一個(gè)混搭的例子。
- (NSMutableArray <CDZTableViewSection *>*)sections{
NSMutableArray <CDZTableViewSection *> *sections = [NSMutableArray array];
CDZTableViewSection *firstSection = [[CDZTableViewSection alloc]init];
NSString *itemA = @"itemA";
[firstSection addItem:itemA cellClass:[TestACell class]];
NSDictionary *itemB = @{@"title" : @"itemB"};
[firstSection addItem:itemB cellClass:[TestACell class]];
TestItem *itemC = [[TestItem alloc]init];
itemC.title = @"itemC";
itemC.tapBlock = ^(TestItem *item) {
NSLog(@"%@ tap",item.title);
};
[firstSection addItem:itemC cellClass:[TestBCell class]];
TestItem *itemD = [[TestItem alloc]init];
itemD.title = @"itemD";
itemD.tapBlock = ^(TestItem *item) {
NSLog(@"%@ tap",item.title);
};
[firstSection addItem:itemD cellClass:[TestBCell class]];
[sections addObject:firstSection];
return sections;
}
所有源碼和Demo
可以直接拿文件去使用或修改担猛,也支持Cocoapods.
如果您覺(jué)得有幫助,不妨給個(gè)star鼓勵(lì)一下,歡迎關(guān)注&交流
有任何問(wèn)題歡迎評(píng)論私信或者提issue
QQ:757765420
Email:nemocdz@gmail.com
Github:Nemocdz
微博:@Nemocdz