美文网首页
UITableView模块式列表处理

UITableView模块式列表处理

作者: youlianchun | 来源:发表于2017-09-13 16:52 被阅读0次

有这么种需求:也个页面由固定的几种模块组成,假设如下图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创建示例:

-(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创建示例:

-(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创建的简单函数:

-(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:代码其实没什么,介绍的是思路。要是有什么类库可以介绍一下。

相关文章

网友评论

      本文标题:UITableView模块式列表处理

      本文链接:https://www.haomeiwen.com/subject/bxhlsxtx.html