「OC」初识MVC —— 简单学习UITableView的解耦

「OC」初识MVC ------ 简单学习UITableView的解耦

文章目录

写在前面

最近在学习了解MVC,然后开始发现,原来之前自己写的内容,真的拿不上一点台面,真是叫人头大啊。好吧😭,知耻而后勇,只能继续好好学习了。由于是刚刚接触MVC,很多内容都是根据网上内容进行相关学习,因为没有基础的概念认知,如果有错误敬请斧正。

认识MVC

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

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

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

解耦

我们先前在使用tableView的时候,往往使用以下代码去设置代理源和数据源

self.tableView.delegate = self;
self.tableView.dataSource = self;

数据源

UITableViewDataSource这之中,我们必须实现的有以下的方法

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

其他可选的内容如下

objc 复制代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 默认情况下如果没有实现,就是1
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // 字体样式是固定的。如果你想使用不同的样式,可以使用自定义视图(UILabel)
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // 编辑
// 单独的行可以选择不设置为可编辑。如果没有实现,所有行都假定为可编辑。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// 移动/重新排序
// 允许为特定的行选择性显示重新排序的辅助视图。默认情况下,只有在数据源实现了 tableView:moveRowAtIndexPath:toIndexPath: 方法时,重新排序控件才会显示。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

// 索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView; // 返回要在节索引视图中显示的节标题列表(例如 "ABCD...Z#")
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // 告诉表格哪个节对应于节标题/索引(例如 "B",1)

// 数据操作 - 插入和删除支持
// 当行调用了减号或加号按钮(基于单元格的 UITableViewCellEditingStyle)后,数据源必须提交更改
// 对于使用 UITableViewRowAction 的编辑操作不调用 - 将调用动作的处理程序
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// 数据操作 - 重新排序/移动支持
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

代理

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

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

这样就有一个问题,就是我们需要在创建每一个tableView的时候,我们实现的内容有很大一部分是重复的内容,我们每次都需要为tableView写关于这个代理和数据源的方法代码,这显然是不符合解耦规则的,而且在这个设置代理源和数据源的时候,代码像是V(即tableView)去持有M(即数据源)和C(代理对象),好似也不太符合MVC的规则,我们可以尝试着将数据源和代理进行分离。

创建cell的基础类

因为每个cell的内容其实都大差不差,所以我们可以使用一个单独的类对cell进行分装成Model,方便我们进行初始化,如果我们遇到特殊需求的cell那么其实我们只要再在分装好的item之中写一个扩展,添加需要的属性即可

objc 复制代码
// JCUITableViewItems.h
#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"
NS_ASSUME_NONNULL_BEGIN

@interface JCUITableViewItems : NSObject

@property (copy, nonatomic) NSString *identifier;
@property (nonatomic, strong) UIImageView *customImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIImageView *iconView;


-(instancetype)initWithImage: (UIImage*)image title:(NSString*)title subtitle:(NSString*)subtitle;
@end

NS_ASSUME_NONNULL_END


// JCUITableViewItems.m
#import "JCUITableViewItems.h"

@implementation JCUITableViewItems

- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle {
    self = [super init];
    if (self) {
        self.ID = [[NSUUID UUID] UUIDString];
        self.customImageView = [[UIImageView alloc] initWithImage:image];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = title;
        self.subtitleLabel = [[UILabel alloc] init];
        self.subtitleLabel.text = subtitle;
        self.iconView = [[UIImageView alloc] init];
    }
    return self;
}

@end

创建section的相关类

和上面一样,其实我们也可以将section设置成为一个类去分装section

但是这样存在一个问题,就是我们的代理和数据源,分别代表着MVC当中的V和M,这样设置使得V持有了M和C,而实际上我们需要的是让C去持有M和V。为了减少在控制器之中的代码量以及使得UITableView的设计更加合理,我们需要想办法来解决这些问题。

objc 复制代码
// JCSectionObject.h
#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"
NS_ASSUME_NONNULL_BEGIN
@class JCUITableViewItems;
@interface JCSectionObject : NSObject

@property (copy,nonatomic) NSString *headerTitle;
@property (copy,nonatomic) NSString *footerTitle;

@property (nonatomic, strong) NSMutableArray<JCUITableViewItems *> *items;

-(instancetype)initWithArray:(NSMutableArray*) items headerTitle: (NSString*) header footerTitle : (NSString *)footer;

@end

NS_ASSUME_NONNULL_END


// JCSectionObject.m
#import "JCSectionObject.h"

@implementation JCSectionObject

-(instancetype)initWithArray:(NSMutableArray*) items headerTitle: (NSString*) header footerTitle : (NSString *)footer {
    self = [super init];
    if (self) {
        _headerTitle = header;
        _footerTitle = footer;
        _items = items;
    }
    return self;
}

- (void)addItem:(JCUITableViewItems *)item {
    [self.items addObject:item];
}

@end

分离数据源

我们为了避免重复内容的出现,我们可以将数据源直接分装成为一个类

objc 复制代码
//JCTableViewDataSource.h
#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"
@class JCSectionObject;
NS_ASSUME_NONNULL_BEGIN

@interface JCTableViewDataSource : NSObject<UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<JCSectionObject *> *sections;

- (void)addSection:(JCSectionObject *)section;

@end

NS_ASSUME_NONNULL_END

//JCTableViewDataSource.m
#import "JCTableViewDataSource.h"
#import "JCUITableViewItems.h"
#import "JCSectionObject.h"
@implementation JCTableViewDataSource

- (instancetype)init {
    self = [super init];
    if (self) {
        _sections = [NSMutableArray array];
    }
    return self;
}

- (void)addSection:(JCSectionObject *)section {
    [self.sections addObject:section];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sections[section].items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JCUITableViewItems *item = self.sections[indexPath.section].items[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:item.identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:item.identifier];
    }
    
    cell.imageView.image = item.customImageView.image;
    cell.textLabel.text = item.titleLabel.text;
    cell.detailTextLabel.text = item.subtitleLabel.text;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sections[section].headerTitle;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return self.sections[section].footerTitle;
}
@end

分离代理

与分离数据源相似,我们也需要对代理之中的内容进行分离,代理之中比较重要的内容就是选中cell,我们需要定义一个block,在代理类之中获取点击的index,然后在控制器之中回调出我们需要的进行的操作

objc 复制代码
//JCUITableViewDelegate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN

@interface JCUITableViewDelegate : NSObject<UITableViewDelegate>
@property (nonatomic, copy) void (^didSelectRowAtIndexPath)(NSIndexPath *indexPath);
@end

NS_ASSUME_NONNULL_END

//JCUITableViewDelegate.m
#import "JCUITableViewDelegate.h"

@implementation JCUITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectRowAtIndexPath) {
        self.didSelectRowAtIndexPath(indexPath);//将参数传入
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 60;
}

@end

经过这样的操作,我们就给控制器实现了简单的瘦身,控制器代码如下,我们做了这样的操作,不仅避免了我们先前在添加headertitle需要进行判断,更加方便且易于维护。

objc 复制代码
#import "ViewController.h"
#import "JCUITableViewItems.h"
#import "JCSectionObject.h"
#import "JCTableViewDataSource.h"
#import "JCUITableViewDelegate.h"
@interface ViewController ()

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) JCTableViewDataSource *dataSource;
@property (nonatomic, strong) JCUITableViewDelegate *delegate;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.dataSource = [[JCTableViewDataSource alloc] init];
    self.delegate = [[JCUITableViewDelegate alloc] init];
    self.delegate.didSelectRowAtIndexPath = ^(NSIndexPath *indexPath) {
        NSLog(@"选择了: %@", indexPath);
    };//通过回调使得index信息能被传回
    self.tableView.dataSource = self.dataSource;
    self.tableView.delegate = self.delegate;
    
    [self addTestData];
    
    [self.tableView reloadData];
}

- (void)addTestData {

    NSMutableArray *section1Items = [NSMutableArray array];
    JCUITableViewItems *item1 = [[JCUITableViewItems alloc] initWithImage:[UIImage imageNamed:@"image1"] title:@"Title 1" subtitle:@"Subtitle 1"];
    JCUITableViewItems *item2 = [[JCUITableViewItems alloc] initWithImage:[UIImage imageNamed:@"image1"] title:@"Title 2" subtitle:@"Subtitle 2"];
    item1.identifier = @"Cell1";
    [section1Items addObject:item1];
    [section1Items addObject:item2];

    JCSectionObject *section1 = [[JCSectionObject alloc] initWithArray:section1Items
                                                           headerTitle:@"Section 1 Header"
                                                           footerTitle:@"Section 1 Footer"];
    

    NSMutableArray *section2Items = [NSMutableArray array];
    JCUITableViewItems *item3 = [[JCUITableViewItems alloc] initWithImage:[UIImage imageNamed:@"image2"] title:@"Title 3" subtitle:@"Subtitle 3"];
    item2.identifier = @"Cell2";
    [section2Items addObject:item3];
    

    JCSectionObject *section2 = [[JCSectionObject alloc] initWithArray:section2Items
                                                           headerTitle:@"Section 2 Header"
                                                           footerTitle:@"Section 2 Footer"];
    

    [self.dataSource addSection:section1];
    [self.dataSource addSection:section2];
}



@end

这样子我们就获得了以下的UITableView

总结

关于tableView的解耦自我感觉还是一知半解,关于MVC和解耦的思想还是不太理解,对于在解耦的tableView之中使用自定义cell的部分还有一定的不理解,但是还是汇总为了博客记录一下学习的内容,后面还有根据网络申请进行分装,那就在后面学到了相关内容再进行深入学习。

参考资料

如何写好一个UITableView

UITableView 解耦

相关推荐
架构文摘JGWZ31 分钟前
Java 23 的12 个新特性!!
java·开发语言·学习
小齿轮lsl35 分钟前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
Aic山鱼1 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
qq11561487071 小时前
Java学习第八天
学习
天玑y1 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
2301_789985942 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
橄榄熊2 小时前
Windows电脑A远程连接电脑B
学习·kind
web_learning_3213 小时前
source insight学习笔记
笔记·学习
Lossya3 小时前
【机器学习】参数学习的基本概念以及贝叶斯网络的参数学习和马尔可夫随机场的参数学习
人工智能·学习·机器学习·贝叶斯网络·马尔科夫随机场·参数学习
Joeysoda4 小时前
Java数据结构 时间复杂度和空间复杂度
java·开发语言·jvm·数据结构·学习·算法