剛進(jìn)公司不到一個(gè)月,接到一個(gè)需求孙蒙,把一個(gè)項(xiàng)目的界面改一下项棠。看了項(xiàng)目里的視圖挎峦,耦合性強(qiáng)香追,沒有復(fù)用,改起UI來很費(fèi)勁坦胶。新的界面是個(gè)列表視圖透典,就尋思怎么寫出一個(gè)后面接手的人改起UI不那么費(fèi)勁的tableview晴楔,順便把項(xiàng)目里老的collectionView也進(jìn)行了類比,偷偷進(jìn)行了重構(gòu)峭咒。畢竟UI總是改來改去的税弃,而常用的tableview和collecitonview更是應(yīng)該讓其擴(kuò)展性更好,復(fù)用性更強(qiáng)讹语。
先從tableview講起钙皮,tableview都很熟悉蜂科,一般就是由datasource顽决,delegate,cell导匣,cellitem這幾部分組成才菠。
打造一個(gè)通用性更強(qiáng)的DataSource
新建一個(gè)NSObject的子類,命名為CDZTableDataSource贡定,并遵循<UITableViewDataSource>協(xié)議赋访。對(duì)于datasource來說,必須實(shí)現(xiàn)的方法是-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
和-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
先看看第一個(gè)方法缓待,返回每個(gè)section的row的數(shù)目蚓耽,這個(gè)數(shù)字怎么來呢,就是section里的數(shù)據(jù)條數(shù)旋炒。如果不實(shí)現(xiàn)optional方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
的話步悠,默認(rèn)只有一個(gè)section。所以我們首先要確定的就是section的數(shù)目瘫镇,所以對(duì)應(yīng)的鼎兽,我們應(yīng)該有section的model。新建一個(gè)NSObject的子類铣除,命名為CDZSectionObject谚咬。那么這個(gè)section應(yīng)該有些什么屬性呢?對(duì)于一個(gè)section對(duì)象來說尚粘,應(yīng)該持有一個(gè)row對(duì)應(yīng)的model的數(shù)組择卦,這樣就可以確定每個(gè)section的row的數(shù)量了±杉蓿可能還有一些sectionheader是數(shù)據(jù)等等互捌。
@property (nonatomic, copy) NSString *headerTitle;
@property (nonatomic, copy) NSString *headerReuseIdentifer;
@property (nonatomic, strong) NSMutableArray *itemsArray;
回到datasouce,那么datasouce就可以加上這些屬性行剂。
@property (nonatomic, strong) NSMutableArray<CDZSectionObject *> *sectionsArray;
@property (nonatomic, strong) CDZSectionObject *firstSection;
那么現(xiàn)在就可以實(shí)現(xiàn)第一個(gè)方法了秕噪,為了方便我們還可以默認(rèn)有一個(gè)firstsection,方便只有一個(gè)section的情況使用厚宰。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.sectionsArray.count > section) {
return self.sectionsArray[section].itemsArray.count;
}
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.sectionsArray ? self.sectionsArray.count : 0;
}
- (CDZSectionObject *)firstSection{
if (!_firstSection) {
_firstSection = [[CDZSectionObject alloc]init];
[self.sectionsArray addObject:_firstSection];
}
return _firstSection;
}
輪到第二個(gè)方法了腌巾,配置cell的時(shí)候遂填,我們需要知道cell對(duì)應(yīng)的item而去配置cell,那么更抽象一點(diǎn)的話澈蝙,也就是說datasource是建立了cellitem和cell之間的聯(lián)系吓坚,也就是Model-View直接的聯(lián)系。View本身做的工作也就是根據(jù)Model配置View的樣子灯荧。所以這時(shí)候我們我們可以定義一個(gè)接口礁击。
@protocol CDZCustomView
@required
- (void)setItem:(id)item;
@end
然后讓我們的Cell遵守這個(gè)協(xié)議,也就是所有通用的Cell逗载,需要在方法內(nèi)實(shí)現(xiàn)item的解析哆窿。而其實(shí)對(duì)于cell也是一種view,這個(gè)接口可以讓一些復(fù)用的view都遵守厉斟,比如headerview等等挚躯。對(duì)于cell我們還可以拓展這個(gè)協(xié)議。
@protocol CDZTableCell <CDZCustomView>
@required
+ (CGFloat)tableView:(UITableView *)tableView rowHeightForItem:(id)item;
@end
這樣擦秽,cell的高度也可以由cell內(nèi)部自己決定码荔,適合各種各樣的cell,當(dāng)然也可以讓autolayout決定感挥。但是有一些老項(xiàng)目里的cell里面沒有autolayout缩搅,所以返回高度通用性可能更強(qiáng)些。
回到datasource触幼,我們解決了cell的model問題硼瓣,下一個(gè)是,cell的class怎么決定呢域蜗?其實(shí)對(duì)于datasouce來說巨双,cell的類型,往往是由對(duì)應(yīng)的model決定的霉祸,也就是model的類型筑累,決定可以使用的cell的種類。那么我們?cè)赿atasouce里丝蹭,只要給出一個(gè)接口慢宗,讓使用者建立這種聯(lián)系,item class - cell class的聯(lián)系奔穿,并在內(nèi)部用一個(gè)類似哈希表的東西儲(chǔ)存起來镜沽,我們便可以根據(jù)item找到cell的class。簡單的話用一個(gè)NSMutableDictionary就可以了贱田。
- (void)setCellClass:(Class)cellClass forItemClass:(Class)itemClass{
if (!itemClass || !cellClass) {
return;
}
[self.cellDic setObject:cellClass forKey:NSStringFromClass(itemClass)];
}
而這樣以后要更換view的時(shí)候缅茉,只要新建一個(gè)cell并重新set一下對(duì)應(yīng)的item的就可以了,換model也類似男摧。
那么現(xiàn)在就可以根據(jù)index找到對(duì)應(yīng)的section和對(duì)應(yīng)的row所對(duì)應(yīng)的item所對(duì)應(yīng)的cellclass了蔬墩。
- (id)tableView:(UITableView *)tableView itemForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.sectionsArray.count > indexPath.section) {
if (self.sectionsArray[indexPath.section].itemsArray.count > indexPath.row) {
return self.sectionsArray[indexPath.section].itemsArray[indexPath.row];
}
}
return nil;
}
- (Class)tableView:(UITableView *)tableView cellClassForRowAtIndexPath:(NSIndexPath *)indexPath{
id item = [self tableView:tableView itemForRowAtIndexPath:indexPath];
Class cellClass = [self.cellDic objectForKey:NSStringFromClass([item class])];
if (!cellClass) {
cellClass = [UITableViewCell class];//沒有對(duì)應(yīng)關(guān)系的译打,取默認(rèn)的UITalbeViewCell
}
return cellClass;
}
那么cellforrow方法就出來了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Class cellClass = [self tableView:tableView cellClassForRowAtIndexPath:indexPath];
UITableViewCell<CDZTableCell> *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellClass)];
if (!cell) {
cell = [[cellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(cellClass)];
}
if ([cell respondsToSelector:@selector(setItem:)]) {//檢查是否有實(shí)現(xiàn)setItem的方法
[cell setItem:[self tableView:tableView itemForRowAtIndexPath:indexPath]];
}
return cell;
}
到此DataSource就完成了。
如何使用呢拇颅?
讓自定義的cell遵守<CDZTableCell>協(xié)議奏司,內(nèi)部實(shí)現(xiàn)setItem方法去配置view。一些老的cell也可以簡單的遵守協(xié)議并把以前配置視圖的方法寫到setItem方法里就可以了樟插。協(xié)議比繼承耦合性更低些韵洋,即加即用。
讓tableview的datasouce指定為一個(gè)CDZTabeDataSource黄锤,并設(shè)置item對(duì)應(yīng)的cell類型就可以搪缨。更換cell和item只要重新建立關(guān)系即可,達(dá)到了datasource的復(fù)用和拓展性猜扮。對(duì)于各種tableview勉吻,就不用重復(fù)寫datasource的代碼了监婶。例子如Demo
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
Class cellClass = [self.tableDataSource tableView:tableView cellClassForRowAtIndexPath:indexPath];
return (cellClass == [UITableViewCell class]) ? 44.f : [cellClass tableView:tableView rowHeightForItem:[self.tableDataSource tableView:tableView itemForRowAtIndexPath:indexPath]];
}
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self.tableDataSource;
}
return _tableView;
}
- (CDZTableDataSource *)tableDataSource{
if (!_tableDataSource) {
_tableDataSource = [[CDZTableDataSource alloc]init];
[_tableDataSource setCellClass:[CDZTableViewCell class] forItemClass:[CDZCellItem class]];
_tableDataSource.firstSection.itemsArray = [[self mockItems] copy];
}
return _tableDataSource;
}
CollecitonViewDatasource其實(shí)也類似
區(qū)別在于collecitonview的cell要注冊(cè)旅赢,所以cellforrow方法有所不同
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Class cellClass = [self collectionView:collectionView cellClassForRowAtIndexPath:indexPath];
UICollectionViewCell<CDZCollectionCell> *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(cellClass) forIndexPath:indexPath];
if ([cell respondsToSelector:@selector(setItem:)]) {
[cell setItem:[self collectionView:collectionView itemForRowAtIndexPath:indexPath]];
}
return cell;
}
其它基本類似,且collectionview和tableview還可以共用一個(gè)sectionobject惑惶。
最后
所有源碼和Demo
工作里煮盼,我們經(jīng)常會(huì)因?yàn)橼s需求而做一些復(fù)制粘貼。但也別忘了多思考如何復(fù)用带污,如何類比僵控,雖然花掉現(xiàn)在一些時(shí)間,卻會(huì)在日后的重構(gòu)鱼冀,修改报破,別人的修改上帶來很多便利。
如果您覺得有幫助,不妨給個(gè)star鼓勵(lì)一下,歡迎關(guān)注&交流
有任何問題歡迎評(píng)論私信或者提issue
QQ:757765420
Email:nemocdz@gmail.com
Github:Nemocdz
微博:@Nemocdz