关于TableView多种cell的处理

来到这家公司之后,接手这个项目中付费页面功能的开发,这个模块是从之前离职员工那里接受的。首先说一些这个页面的复杂程序,这个页面会根据不同的情况展示三个不同的页面,这个页面整体是采用TableView来写的,三个页面加起来总共有18中cell样式。在接手的时候,由于整个项目采用的是TableView,但是却没有使用重用机制,整个页面在滑动起来比较卡顿。

1.第一步优化,如果根据不同的情况展示不同的Cell

我们的项目采用的是MVVM的设计模式,重新对tableView代理方法返回行数,以及返回那些Cell做了一层重构。首先把每种cell定义成枚举。

typedef NS_ENUM(NSUInteger, YCIAPUITableViewCellStyle) {
    YCIAPUITableViewCellStyleVipHeaderViewCell = 0, // vip黄色头部view
    YCIAPUITableViewCellStyleGainCircumViewCell, // 周边
    YCIAPUITableViewCellStyleCourseIntroductionViewCell, // 课程介绍
    YCIAPUITableViewCellStyleGoodsListViewCell, // 商品
    YCIAPUITableViewCellStyleMoreVipPrivilegeViewCell, // vip特权
    YCIAPUITableViewCellStyleStudyReportViewCell, // 学情报告
    YCIAPUITableViewCellStyleTryForFreeViewCell, // 体验一下
    YCIAPUITableViewCellStyleMyVipAssistantViewCell,// 会员客服
    YCIAPUITableViewCellStylePaymentSwitchCell, // 切换金牌课程
    YCIAPUITableViewCellStyleCircumIntroductionViewCell,//周边介绍
    YCIAPUITableViewCellStyleIAPEmptyCell,//空cell
    YCIAPUITableViewCellStyleGoldenHeaderViewCell, // 非会员头部
    YCIAPUITableViewCellStyleAnimationThemeViewCell, // 全动画
    YCIAPUITableViewCellStyleTeamIntroduceCell, // 团队
    YCIAPUITableViewCellStyleCoursePredictViewCell, //课程预告
    YCIAPUITableViewCellStyleLearningPhaseCell, // 课程学习阶段
    YCIAPUITableViewCellStyleEfficientScoreCell, // 高效学习
};

然后定义三个数组分别去存放,三中不同情况下,应该展示的cell的样式,我这里做的处理是把枚举放到数组中。为什么要这样处理呢,就是在tableview代理方中,可以通过枚举来加载不同的cell

      
        self.vipArray = [NSMutableArray arrayWithObjects:
                         @(YCIAPUITableViewCellStyleVipHeaderViewCell),
                         @(YCIAPUITableViewCellStyleStudyReportViewCell),
                         @(YCIAPUITableViewCellStyleGoodsListViewCell),
                         @(YCIAPUITableViewCellStyleMoreVipPrivilegeViewCell),
                         @(YCIAPUITableViewCellStyleMyVipAssistantViewCell),
                         nil
                         ];
        
        self.noVipArray = [NSMutableArray arrayWithObjects:
                           @(YCIAPUITableViewCellStyleGoldenHeaderViewCell),
                           @(YCIAPUITableViewCellStyleCourseIntroductionViewCell),
                           @(YCIAPUITableViewCellStyleTeamIntroduceCell),
                           @(YCIAPUITableViewCellStyleAnimationThemeViewCell),
                           @(YCIAPUITableViewCellStyleLearningPhaseCell),
                           @(YCIAPUITableViewCellStyleEfficientScoreCell),
                           @(YCIAPUITableViewCellStyleMoreVipPrivilegeViewCell),
                           @(YCIAPUITableViewCellStyleGoodsListViewCell),
                           @(YCIAPUITableViewCellStyleTryForFreeViewCell),
                           nil
                           ];
        
        self.switchArray = [NSMutableArray arrayWithObjects:
                            @(YCIAPUITableViewCellStyleGoldenHeaderViewCell),
                            @(YCIAPUITableViewCellStyleCourseIntroductionViewCell),
                            @(YCIAPUITableViewCellStyleTeamIntroduceCell),
                            @(YCIAPUITableViewCellStyleAnimationThemeViewCell),
                            @(YCIAPUITableViewCellStyleLearningPhaseCell),
                            @(YCIAPUITableViewCellStyleEfficientScoreCell),
                            @(YCIAPUITableViewCellStyleMoreVipPrivilegeViewCell),
                            @(YCIAPUITableViewCellStyleCoursePredictViewCell),
                            @(YCIAPUITableViewCellStylePaymentSwitchCell),
                            nil
                            ];

接下来问题来了,对于每一种情况,展示的cell的个数又是不确定的,怎么保证,只展示需要展示的cell呢。 这个地方我偷了一个懒或者采用另外一种解决方法,就是把不需要展示的cell的高度设置成0。下面的这段代码写在VM中

- (CGFloat)cellHeight:(NSIndexPath *)indexPath{
    if (self.switchType == YCIAPPaymentViewModelSwitchOn) {
        if (self.isVip) { // 会员
            // 学情报告解锁
            if (indexPath.row == 1) {
                if (!self.unlockLearningReport) { // 学情报告
                    return 0;
                }else{
                    return  UITableViewAutomaticDimension;
                }
            }else if (indexPath.row == 2) {
                if (!self.ycPay || !self.ycRenewPay) { // 商品列表
                    return 0;
                }else{
                    return  UITableViewAutomaticDimension;
                }
            }else{
                return  UITableViewAutomaticDimension;
            }
        }else{ // 引导付费
            if (indexPath.row == 7) {
                if (!self.ycPay || !self.ycRenewPay) {  // 商品列表
                    return 0;
                }else{
                    return  UITableViewAutomaticDimension;
                }
            }else if (indexPath.row == 8){
                if (!self.hasTryForFreeRights) {  // 免费体验
                    return 0;
                }else{
                    return  UITableViewAutomaticDimension;
                }
            }else{
                return  UITableViewAutomaticDimension;
            }
        }
    }else{  // 开关外
      if (indexPath.row == 8) {
            if (self.isVip) {
                return UITableViewAutomaticDimension;
            }else{
                return 0;
            }
        }
        return  UITableViewAutomaticDimension;
    }
    return 0;
}

这种方法虽然不是最优解,但是相比每一种情况又要单独处理,每一个cell是否显示,还要单独处理显示cell以及不显示cell的高度,显得更加方便。
如果确定tableView应该展示几行呢,这个时候,我定义的三个数组就发挥作用了。

- (NSArray *)getVipArray{
    return [self.vipArray copy];
}
- (NSArray *)getNoVipArray{
    return [self.noVipArray copy];
}
- (NSArray *)getSwitchArray{
    return [self.switchArray copy];
}
2.第二步优化,在控制器的tableView方法中该怎么使用呢?
// MARK:引导付费的cell
- (UITableViewCell *)loadTableCellOfNoVipWithIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > [[self.viewModel getNoVipArray] count]) {
        return [[UITableViewCell alloc] init];
    }
    NSInteger cellStyleIndex =  [[[self.viewModel getNoVipArray] objectAtIndex:indexPath.row] integerValue];
    return [self cellWithType:cellStyleIndex];
}

// MARK:会员的cell
- (UITableViewCell *)loadTableCellOfVipWithIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > [[self.viewModel getVipArray] count]) {
        return [[UITableViewCell alloc] init];
    }
    NSInteger cellStyleIndex =  [[[self.viewModel getVipArray] objectAtIndex:indexPath.row] integerValue];
    return [self cellWithType:cellStyleIndex];
}
// 返回开关外,课程预告 cell
- (UITableViewCell *)loadSwitchOffPredictionCellWithIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > [[self.viewModel getSwitchArray] count]) {
        return [[UITableViewCell alloc] init];
    }
    NSInteger cellStyleIndex =  [[[self.viewModel getSwitchArray] objectAtIndex:indexPath.row] integerValue];
    return [self cellWithType:cellStyleIndex];
}
3.第三步优化,解决tableView重用问题

可以先看一段之前的代码,这是之前返回cell的方法,if else 判断有几十个。这个代码已经难以维护,只要有新的需求就要改动很多地方。

// MARK:引导付费的cell
- (UITableViewCell *)loadTableCellOfNoVipWithIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        YCGoldenHeaderViewCell *cell = [[YCGoldenHeaderViewCell alloc]init];
        return cell;
    }else if (indexPath.row == 1) {
        @weakify(self);
        YCCourseIntroductionViewCell *introductiongView = [[YCCourseIntroductionViewCell alloc]initWithCourseDesignClick:^(YCCourseIntroductionViewButtonClickType type) {
            @strongify(self);
            
            [self showAlertWithType:type];
        }];
        return introductiongView;
    }else if (indexPath.row == 2){
        if (self.viewModel.ycRenewPay && self.viewModel.ycPay) {
            
            YCGoodsListViewCell *goodListView = [[YCGoodsListViewCell alloc] initWithGoodModelList:self.viewModel.goodsModelList payButtonClickBlock:[self returnClickPaymentButtonBlockWithPostionIsPage:YES] agreementClickBlock:[self pushToAgreementVC] agreementHidden:NO];
            return goodListView;
        }else if (self.viewModel.prizeActivity) {
            // 周边介绍
            YCCircumIntroductionViewCell *circumView = [[YCCircumIntroductionViewCell alloc] init];
            return circumView;

        }else {
            // VIP特权
            YCMoreVipPrivilegeViewCell *privilegeView = [[YCMoreVipPrivilegeViewCell alloc]initWithIsBottom:[self.viewModel judgeMoreVipPrivilegeViewIsBottom]];
            return privilegeView;
        }
        
    }else if (indexPath.row == 3){
        if (self.viewModel.ycRenewPay && self.viewModel.ycPay) {
            if (self.viewModel.prizeActivity) {
                // 周边介绍
                YCCircumIntroductionViewCell *circumView = [[YCCircumIntroductionViewCell alloc] init];
                return circumView;
            }else {
                // VIP特权
                YCMoreVipPrivilegeViewCell *privilegeView = [[YCMoreVipPrivilegeViewCell alloc]initWithIsBottom:[self.viewModel  judgeMoreVipPrivilegeViewIsBottom]];
                return privilegeView;
            }
        }else if (self.viewModel.prizeActivity) {
            // VIP特权
            YCMoreVipPrivilegeViewCell *privilegeView = [[YCMoreVipPrivilegeViewCell alloc]initWithIsBottom:[self.viewModel  judgeMoreVipPrivilegeViewIsBottom]];
            return privilegeView;
        }else {
            if (self.viewModel.unlockLearningReport) {

                @weakify(self);
                YCStudyReportViewCell *studyView = [[YCStudyReportViewCell alloc] initWithCheckStudyReportButtonClickBlock:^{
                    @strongify(self);
                    [self pushToStudyReportVC];
                } isBottom:[self.viewModel  judgeStudyReportViewIsBottom] isVip:self.viewModel.isVip];
                return studyView;
            }else {
                // 体验一下
                @weakify(self);
                YCTryForFreeViewCell *tryForFreeView = [[YCTryForFreeViewCell alloc]initWithTryForFreeButtonClickBlock:^{
                    @strongify(self);
                    [self pushToTopicVCTryForFree];
                } hasGreyBottom:[self.viewModel judgeHasPayButtonOnBottom]];
                return tryForFreeView;

            }
        }
    }else if (indexPath.row == 4){
        if (self.viewModel.ycRenewPay && self.viewModel.ycPay && self.viewModel.prizeActivity) {
   ………………

大家可以对比一下下面的返回cell的方法,是不是感觉清爽了好多,对于一些逻辑处理都放在ViewModel中做处理了。如果有新的需求变化,比如添加新的cell或者减少cell直接在viewModel中操纵cell的数据就可以了。

在cellWithType:cellStyleIndex这个方法中,就是通过传入的枚举类型,返回不同的cell
- (UITableViewCell *)cellWithType:(YCIAPUITableViewCellStyle)style{
    switch (style) {
        case YCIAPUITableViewCellStyleVipHeaderViewCell:{ // vip头部视图
            YCVipHeaderViewCell *vipHeaderViewCell = [self.tableView dequeueReusableCellWithIdentifier:[YCVipHeaderViewCell identifier]];
            vipHeaderViewCell.feedbackBlock = [self returnFeedbackBlock];
            return vipHeaderViewCell;
        }
            break;
        case YCIAPUITableViewCellStyleGainCircumViewCell: { // 抱枕
            YCGainCircumViewCell *gainCircumViewCell = [self.tableView dequeueReusableCellWithIdentifier:[YCGainCircumViewCell identifier]];
            return gainCircumViewCell;
        }
            break;
        case YCIAPUITableViewCellStyleCourseIntroductionViewCell:{ // 课程介绍
            YCCourseIntroductionViewCell *introductiongViewCell = [self.tableView dequeueReusableCellWithIdentifier:[YCCourseIntroductionViewCell identifier]];
            return introductiongViewCell;
        }
            break;
        case YCIAPUITableViewCellStyleGoodsListViewCell: { // 商品列表
            YCGoodsListViewCell *goodListViewCell = [self.tableView dequeueReusableCellWithIdentifier:[YCGoodsListViewCell identifier]];
            goodListViewCell.goodsList = self.viewModel.goodsModelList;
            goodListViewCell.payButtonBlock = [self returnClickPaymentButtonBlockWithPostionIsPage:YES];
            goodListViewCell.agreementBlock = [self pushToAgreementVC];
            goodListViewCell.questionClickBlock = [self pushToQuestionVC];
            goodListViewCell.agreementHidden = NO;
            goodListViewCell.isVip = self.viewModel.isVip;
            goodListViewCell.isBottom = [self.viewModel judgeGoodsListIsBottom];
            [goodListViewCell reloadData];
            return goodListViewCell;
        }
            break;
……………………

在解决返回cell逻辑复杂的同时,我也顺被把cell的重用解决了一下,这个地方就不粘贴出来了,只要把每一个cell注册一下,准守tableView的注册机制就可了。难度在怎么把原来的cell直接初始化数据来填充布局,改成数据回来之后,再去更新cell的布局,这个大家都有经验,我就不说了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容