如何处理一个tableView中同种model多种cell相同逻辑的情况?

2017 年 11 月 14 日 CocoaChina 无夜之星辰


IU的图片用完了,用我允的代替一下


这是购物车页面:



有4种cell:


1.一般商品cell

2.带赠品的商品cell

3.满赠商品cell

4.补货中商品cell


一般来说,有多少种cell就要自定义多少种cell,但是这4种cell又有相同的逻辑处理,如点击商品图片进入商品详情页。如何处理既不会让代码显得啰嗦又不会因为继承导致耦合度变高?


我的做法是先封装一个基类cell,这个cell只封装逻辑处理相关代码


#import <UIKit/UIKit.h>

#import "CQShopCartCellModel.h"


@class CQShopCartGoodsCell;

@protocol CQShopCartGoodsCellDelegate <NSObject>


@optional


/** 选中按钮点击 */

- (void)goodsCell:(CQShopCartGoodsCell *)goodsCell chooseButtonDidClick:(UIButton *)chooseButton;

/** 加按钮点击 */

- (void)goodsCell:(CQShopCartGoodsCell *)goodsCell addButtonDidClick:(UIButton *)addButton;

/** 减按钮点击 */

- (void)goodsCell:(CQShopCartGoodsCell *)goodsCell minusButtonDidClick:(UIButton *)minusButton;

/** 商品图片点击 */

- (void)goodsCell:(CQShopCartGoodsCell *)goodsCell goodsImageViewDidTap:(UIImageView *)goodsImageView;


@end


@interface CQShopCartGoodsCell : UITableViewCell


@property (nonatomic, weak) id <CQShopCartGoodsCellDelegate> delegate;

@property (nonatomic, strong) CQShopCartCellModel *model;


@end


然后4种cell再继承这个基类cell。


在controller中:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CQShopCartGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@""];

    CQShopCartCellModel *model = nil;

    switch (indexPath.section) {

        case 0: // 普通商品

        {

            model = self.commonGoodsArray[indexPath.row];

            if (goodsCell == nil) {

                if (model.giftsArray.count > 0) {

                    // 有赠品的商品

                    goodsCell = [[CQShopCartHaveGiftGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CQShopcartHaveGiftGoodsCellID];

                } else {

                    // 无赠品的商品

                    goodsCell = [[CQShopCartNoGiftGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CQShopcartNoGiftGoodsCellID];

                }

            }

        }

            break;

            

        case 1: // 满赠商品

        {

            model = self.giftGoodsArray[indexPath.row];

            goodsCell = (goodsCell ?: [[CQShopCartGiftGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CQShopcartGiftGoodsCellID]);

        }

            break;

            

        case 2: // 补货中商品

        {

            model = self.emptyGoodsArray[indexPath.row];

            goodsCell = (goodsCell ?: [[CQShopCartEmptyGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CQShopcartEmptyGoodsCellID]);

        }

            break;

            

        default:

            break;

    }

    goodsCell.model = model;

    goodsCell.delegate = self;

    return goodsCell;

}



一目了然。


欢迎大家说出自己的想法。


登录查看更多
0

相关内容

还在修改博士论文?这份《博士论文写作技巧》为你指南
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
164+阅读 · 2020年5月14日
【斯坦福大学-论文】实体上下文关系路径的知识图谱补全
【强化学习资源集合】Awesome Reinforcement Learning
专知会员服务
97+阅读 · 2019年12月23日
机器学习入门的经验与建议
专知会员服务
94+阅读 · 2019年10月10日
你头疼的ELK难题,本文几乎都解决了
DBAplus社群
8+阅读 · 2019年3月20日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
DiscuzX 3.4 Phar反序列化漏洞
黑客工具箱
8+阅读 · 2019年1月4日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
如何使用注意力模型生成图像描述?
AI研习社
9+阅读 · 2018年8月6日
Advances in Online Audio-Visual Meeting Transcription
Arxiv
4+阅读 · 2019年12月10日
Arxiv
3+阅读 · 2019年3月1日
Arxiv
9+阅读 · 2016年10月27日
Arxiv
5+阅读 · 2015年9月14日
VIP会员
相关资讯
你头疼的ELK难题,本文几乎都解决了
DBAplus社群
8+阅读 · 2019年3月20日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
DiscuzX 3.4 Phar反序列化漏洞
黑客工具箱
8+阅读 · 2019年1月4日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
如何使用注意力模型生成图像描述?
AI研习社
9+阅读 · 2018年8月6日
Top
微信扫码咨询专知VIP会员