IOS---------Controller瘦身一:剥离UITableView,封装dataSource和deleagte。

MVC

在讨论解耦之前,我们要弄明白 MVC 的核心:控制器(以下简称 C)负责模型(以下简称 M)和视图(以下简称 V)的交互。

这里所说的 M,通常不是一个单独的类,很多情况下它是由多个类构成的一个层。最上层的通常是以 Model结尾的类,它直接被 C 持有。Model类还可以持有两个对象:

  1. Item:它是实际存储数据的对象。它可以理解为一个字典,和 V 中的属性一一对应
  2. Cache:它可以缓存自己的 Item(如果有很多)

常见的误区:

  1. 一般情况下数据的处理会放在 M 而不是 C(C 只做不能复用的事)
  2. 解耦不只是把一段代码拿到外面去。而是关注是否能合并重复代码, 并且有良好的拖展性。

原始版

在 C 中,我们创建 UITableView对象,然后将它的数据源和代理设置为自己。也就是自己管理着 UI 逻辑和数据存取的逻辑。在这种架构下,主要存在这些问题:

  1. 违背 MVC 模式,现在是 V 持有 C 和 M。
  2. C 管理了全部逻辑,耦合太严重。
  3. 其实绝大多数 UI 相关都是由 Cell 而不是 UITableView自身完成的。

为了解决这些问题,我们首先弄明白,数据源和代理分别做了那些事。

数据源

它有两个必须实现的代理方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

简单来说,只要实现了这个两个方法,一个简单的 UITableView对象就算是完成了。

除此以外,它还负责管理 section的数量,标题,某一个 cell的编辑和移动等。

代理

代理主要涉及以下几个方面的内容:

  1. cell、headerView 等展示前、后的回调。
  2. cell、headerView 等的高度,点击事件。

最常用的也是两个方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

提醒:绝大多数代理方法都有一个 indexPath参数

 

所以我们的目的就出来了

1、进行解耦

2、给c专业瘦身

 

做法:对数据源和代理都进行可复用的封装

1、dataSource的封装

.h文件中

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef void (^TableViewCellConfigureBlock)(id cell, id items, NSIndexPath * indexPath);

@interface GMTableViewProtocol : NSObject<UITableViewDataSource,UICollectionViewDataSource>

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;

- (id)itemAtIndexPath:(NSIndexPath *)indexPath;

@end

.m文件中

#import "GMTableViewProtocol.h"

@interface GMTableViewProtocol ()

@property(nonatomic, strong) NSArray* items;/**< array */
@property(nonatomic, copy) NSString* cellIdentifier;/**< cellIdentifier */
@property(nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;/**< block */

@end

@implementation GMTableViewProtocol

- (instancetype)init {
    return  nil;
}

- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock {
    
    self = [super init];
    if (self) {
        self.items = anItems;
        self.cellIdentifier = aCellIdentifier;
        self.configureCellBlock = aConfigureCellBlock;
    }
    return  self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self isDoubleDimensionalArray]) {
        NSArray *sectionArr = self.items[indexPath.section];
        return sectionArr.count > indexPath.row ? sectionArr[(NSUInteger) indexPath.row] : 0;
    }else{
        return self.items.count > indexPath.section ? self.items[(NSUInteger) indexPath.section] : 0;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count > 0 ? self.items.count : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self isDoubleDimensionalArray]) {
        NSArray *sectionArr = self.items[section];
        return sectionArr.count;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    id item = [self itemAtIndexPath:indexPath];
    self.configureCellBlock(cell, item, indexPath);
    return cell;
}

///判断数组是否为二维数组
- (BOOL)isDoubleDimensionalArray
{
    if (self.items.count == 0) return NO;
    if ([self.items.firstObject isKindOfClass:[NSArray class]]) {
        return YES;
    }else{
        return NO;
    }
}

@end

2、delegate的封装

.h文件中

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^GMTableViewDidSelectBlock)(UITableView *GMTableView, NSIndexPath *GMIndexPath);

@interface GMTableViewDelegate : NSObject<UITableViewDelegate>

- (id)initWithHeaderV_section:(UIView *_Nullable)headerV footerV_section:(UIView *_Nullable)footerV rowHeight:(CGFloat)rowH headerH_section:(CGFloat)headerH footerH_section:(CGFloat)footerH didSelectBlock:(GMTableViewDidSelectBlock)didSelectBlock;

@end

.m文件中

#import "GMTableViewDelegate.h"

@interface GMTableViewDelegate ()

@property (nonatomic, strong)UIView *headerV_section;

@property (nonatomic, strong)UIView *footerV_section;

@property (nonatomic, assign)CGFloat rowHeight;

@property (nonatomic, assign)CGFloat headerH_section;

@property (nonatomic, assign)CGFloat footerH_section;

@property (nonatomic, copy)GMTableViewDidSelectBlock didSelectBlock;

@end

@implementation GMTableViewDelegate

- (instancetype)init
{
    return nil;
}

- (id)initWithHeaderV_section:(UIView *)headerV footerV_section:(UIView *)footerV rowHeight:(CGFloat)rowH headerH_section:(CGFloat)headerH footerH_section:(CGFloat)footerH didSelectBlock:(GMTableViewDidSelectBlock)didSelectBlock
{
    self = [super init];
    if (self) {
        self.headerH_section = headerH;
        self.headerV_section = headerV;
        self.footerH_section = footerH;
        self.footerV_section = footerV;
        self.rowHeight       = rowH;
        self.didSelectBlock  = didSelectBlock;
    }
    return self;
}

#pragma mark - <delegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rowHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.headerH_section;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.footerH_section;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerV = [[UIView alloc]init];
    if (self.headerV_section) {
        self.headerV_section.frame = CGRectMake(0, 0, self.headerV_section.width, self.headerV_section.height);
        headerV.size = CGSizeMake(self.headerV_section.width, self.headerV_section.height);
        headerV.backgroundColor = [UIColor whiteColor];
        [headerV addSubview:self.headerV_section];
    }
    return headerV;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerV = [[UIView alloc]init];
    if (self.footerV_section) {
        footerV = [self XC_copyAView:self.footerV_section];
    }
    return footerV;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.didSelectBlock(tableView, indexPath);
}


///深复制UIView
- (UIView *)XC_copyAView:(UIView *)view
{
    NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:view];
    return [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
}

@end

 

如何使用:

    //
    TableViewCellConfigureBlock configureBlock = ^(GMActivityCenterTableViewCell *cell, NSString *img) {
        [cell setCellImg:img];
    };
    self.dataSource = [[GMTableViewProtocol alloc]initWithItems:@[@"activityCenter_luckyDraw",@"activityCenter_popuparize",@"activityCenter_stockGod"] cellIdentifier:cellID configureCellBlock:configureBlock];
    self.activityTableV.dataSource = self.dataSource;
    //
    GMTableViewDidSelectBlock didSelectBlock = ^(UITableView *GMTableView, NSIndexPath *GMIndexPath){
        [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"click %ld",(long)GMIndexPath.section]];
    };
    UIView *footerV = [[UIView alloc]init];
    footerV.backgroundColor = [UIColor whiteColor];
    footerV.size = CGSizeMake(SCREEN_WIDTH, 16*kScreenProportionY);
    CGFloat rowH = (SCREEN_WIDTH - 32)/343*100;
    self.delegate = [[GMTableViewDelegate alloc]initWithHeaderV_section:nil footerV_section:footerV rowHeight:rowH headerH_section:CGFLOAT_MIN footerH_section:16*kScreenProportionY didSelectBlock:didSelectBlock];
    self.activityTableV.delegate = self.delegate;

 

完结,这样就可以很大程度上的减少controller上的代码量,UICollectionView也是一样的类比过去就OK

重点:这是本人根据自己的实际项目需求以及个人代码风格写出来的,各位可根据个人需求以及风格适当修改即可,如有更好的给c瘦身方案也请不吝赐教。最后希望各位看官给个点赞谢谢

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值