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

97 篇文章 2 订阅

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

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

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

一般情况下数据的处理会放在 M 而不是 C(C 只做不能复用的事)
解耦不只是把一段代码拿到外面去。而是关注是否能合并重复代码, 并且有良好的拖展性。
原始版
在 C 中,我们创建 UITableView对象,然后将它的数据源和代理设置为自己。也就是自己管理着 UI 逻辑和数据存取的逻辑。在这种架构下,主要存在这些问题:

违背 MVC 模式,现在是 V 持有 C 和 M。
C 管理了全部逻辑,耦合太严重。
其实绝大多数 UI 相关都是由 Cell 而不是 UITableView自身完成的。
为了解决这些问题,我们首先弄明白,数据源和代理分别做了那些事。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
简单来说,只要实现了这个两个方法,一个简单的 UITableView对象就算是完成了。

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

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

cell、headerView 等展示前、后的回调。
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瘦身方案也请不吝赐教。最后希望各位看官给个点赞谢谢
————————————————
版权声明:本文为CSDN博主「码农--xc」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_33226881/article/details/86635136

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS-RATreeView是一个开源的第三方库,提供了多层级的UITableView展示功能。使用该库可以轻松实现多级列表的展开与收起。 首先,在项目中引入iOS-RATreeView库。可以使用CocoaPods引入,也可以手动下载并导入。 接下来,在需要使用多级列表的UIViewController中,创建一个RADataObject类型的数组,用来存储数据。RADataObject是iOS-RATreeView中的一个数据模型,用来表示一条记录。每个RADataObject可以包含多个子RADataObject,从而形成多级列表。 ``` // 创建RADataObject数组 NSMutableArray *data = [NSMutableArray array]; // 创建一级列表 RADataObject *level1_1 = [RADataObject dataObjectWithName:@"Level 1-1" children:nil]; RADataObject *level1_2 = [RADataObject dataObjectWithName:@"Level 1-2" children:nil]; RADataObject *level1_3 = [RADataObject dataObjectWithName:@"Level 1-3" children:nil]; // 创建二级列表 RADataObject *level2_1 = [RADataObject dataObjectWithName:@"Level 2-1" children:nil]; RADataObject *level2_2 = [RADataObject dataObjectWithName:@"Level 2-2" children:nil]; RADataObject *level2_3 = [RADataObject dataObjectWithName:@"Level 2-3" children:nil]; // 将二级列表添加到一级列表中 level1_1.children = @[level2_1, level2_2]; level1_2.children = @[level2_3]; // 将一级列表添加到RADataObject数组中 [data addObject:level1_1]; [data addObject:level1_2]; [data addObject:level1_3]; ``` 创建完数据源后,需要创建RATreeView对象,并设置代理和数据源。 ``` // 创建RATreeView对象 self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; // 设置代理和数据源 self.treeView.delegate = self; self.treeView.dataSource = self; ``` 接下来实现RATreeViewDataSource协议中的方法,用来返回列表的数据。具体实现可以参考下面的代码。 ``` - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { static NSString *identifier = @"Cell"; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 获取RADataObject对象 RADataObject *dataObject = item; // 设置cell的文本 cell.textLabel.text = dataObject.name; return cell; } - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点数量 return self.data.count; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点数量 return dataObject.children.count; } } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点 return self.data[index]; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点 return dataObject.children[index]; } } - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { // 返回是否可以编辑 return YES; } - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除节点 RADataObject *parentObject = [treeView parentForItem:item]; if (parentObject) { NSMutableArray *children = [NSMutableArray arrayWithArray:parentObject.children]; [children removeObject:item]; parentObject.children = children; } else { NSMutableArray *data = [NSMutableArray arrayWithArray:self.data]; [data removeObject:item]; self.data = data; } // 刷新列表 [treeView reloadData]; } } ``` 最后,在RATreeViewDelegate协议中实现展开与收起节点的方法。 ``` - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = YES; } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = NO; } ``` 至此,多级列表展开与收起的功能就实现了。在需要展示多级列表的地方,只需要将创建的RATreeView添加到视图中即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值