有這么種需求:也個頁面由固定的幾種模塊組成鸥拧,假設如下圖ABCDE 5個模塊收壕,有些時候僅有AB模塊,有些時候是ABC模塊琅摩,app版本2需要在上一版本中去掉或者增加些個模塊或者調整模塊之間的順序此迅。
這種情況下如果沒有一種方式來控制汽畴,那么就坑大了,table的代理好幾個耸序,需要你各個代理內各種條件判斷整袁,每次需求變動后個種改...我接手過這種代碼,我是很寧亂佑吝。
這里介紹的方式是針對section來處理的,且不設計到手勢刪除什么的绳匀,沒測試過芋忿,當待補充吧。
怎么搞疾棵,對table封裝一下戈钢?NO,是搞個轉接對象是尔,這個對象的功能是負責粘合table代理的那些個方法之間的邏輯同步殉了,簡單來說就是指向同一個對象控制,然后代理通過這個轉接對象來獲取原本需要的數據拟枚。
原理介紹完了薪铜,介紹一下使用:
table的各種代理:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.group.numberOfSections;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.group numberOfRowsInSection:section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return [self.group heightForHeaderInSection:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self.group cellForRowAtIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.group didSelectRowAtIndexPath:indexPath];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [self.group viewForHeaderInSection:section];
}
group創(chuàng)建示例:
-(void)reloadData {
self.group = [[AYListGroup alloc] init];
[self.group addObject:[self section_QDHD]];
[self.group addObject:[self section_QDJB]];
[self.group addObject:[self section_GYQD]];
[self.group addObject:[self section_RYQ]];
[self.group addObject:[self section_QDCY]];
[self.group addObject:[self section_LCB]];
[self.group addObject:[self section_QDXC]];
[self.tableView reloadData];
}
section創(chuàng)建示例:
-(AYSection*)section_QDHD {
AYSection *section = [[AYSection alloc] init];
section.titleForHeider = @"ttle";
section.heightForHeader = CommonSectionHeaderViewHeight;
section.hidden = !self.isInTeam || ([self.roughlyStateOfUteam.uactivityEnrollsCount integerValue]==0 && self.roughlyStateOfUteam.uactivityEnrolls.count==0);
__weak typeof(self) wself = self;
[section numberOfRows:nil cellForRow:^UITableViewCell *(NSUInteger index, CGFloat height, AYSection *section) {
if (wself.roughlyStateOfUteam.uactivityEnrolls.count==0) {
return [wself emptyCellWithContent:section.flag leightContent:nil];
}else{
MineActivityCell *cell = [wself.tableView dequeueReusableCellWithIdentifier:MineActivityCellID];
[cell config:wself.roughlyStateOfUteam.uactivityEnrolls[0] indexPath:nil];
return cell;
}
} height:nil didSelect:^(NSUInteger index, AYSection *section) {
if (wself.roughlyStateOfUteam.uactivityEnrolls.count>0){
MyActivityModel *model = wself.roughlyStateOfUteam.uactivityEnrolls[0];
//已結束單獨用一種鏈接
NSInteger status = 2;
if ([model.activityStatus isEqualToString:@"4"]) {
status = 3;
} else {
status = 2;
}
FlowWebViewController *fWebVC = [FlowWebViewController ApplyYueZhanWithParams:nil duelId:nil applyId:model.keyId status:status];
[wself presentFlowViewController:fWebVC animated:YES];
}
} didDeselect:nil];
[section viewForHeader:^UIView *(AYSection *section) {
CommonSectionHeaderView *viewHeader = [wself.tableView dequeueReusableHeaderFooterViewWithIdentifier:CommonSectionHeaderView_reuseIdentifier];
UIImage *subImage = (wself.roughlyStateOfUteam.uactivityEnrolls.count>1 || (wself.roughlyStateOfUteam.uactivityEnrolls.count <=1 && [wself.roughlyStateOfUteam.uactivityEnrollsCount integerValue]>0))?[UIImage imageNamed:@"gengduo"]:nil;
[viewHeader styleWithTitle:section.titleForHeider subTitle:nil subImage:subImage action:^{
if (wself.roughlyStateOfUteam.uactivityEnrolls.count>1 || (wself.roughlyStateOfUteam.uactivityEnrolls.count <=1 && [wself.roughlyStateOfUteam.uactivityEnrollsCount integerValue]>0)) {
MineActivitySubVC * activitySubVC = [MineActivitySubVC MineActivity_TeamInitiateWithId:wself.teamId];
activitySubVC.title = @"title2";
[wself.navigationController pushViewController:activitySubVC animated:YES];
}
}];
return viewHeader;
}];
return section;
}
section示例 是項目實際代碼众弓,有點多,試想一下有5個不固定這種邏輯的模塊來各種組合隔箍,5個不多吧谓娃,我這是7個,坑不是有點大蜒滩,是很大很大滨达。
下邊貼一下section創(chuàng)建的簡單函數:
-(AYSection*)section {
AYSection *section = [[AYSection alloc] init];
section.titleForHeider = @"標題";
section.flag = @"其它跟隨標記或副標題";
section.heightForHeader = 30;
section.hidden = YES;//顯示隱藏
__weak typeof(self) wself = self;
[section numberOfRows:^NSUInteger(AYSection * _Nonnull section) {
return 10;
} cellForRow:^UITableViewCell * _Nonnull(NSUInteger index, CGFloat height, AYSection * _Nonnull section) {
UITableViewCell *cell = [wself.tableView dequeueReusableCellWithIdentifier:@"Identifier"];
return cell;
} height:^CGFloat(NSUInteger index, AYSection * _Nonnull section) {//nil時候采用defaultCellHeight
return 20;
} didSelect:^(NSUInteger index, AYSection * _Nonnull section) {
//...
} didDeselect:^(NSUInteger index, AYSection * _Nonnull section) {
//...
}];
return section;
}
是不是清爽了許多。
這樣一來俯艰,模塊式table的各個模塊內處理邏輯就互不干擾了捡遍,隨便增刪改順序。
到這里想必你已經明白了我的思路了竹握。
ps:代碼其實沒什么画株,介紹的是思路。要是有什么類庫可以介紹一下涩搓。