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

2024-08-31 02:36

本文主要是介绍「OC」初识MVC —— 简单学习UITableView的解耦,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

文章目录

  • 「OC」初识MVC —— 简单学习UITableView的解耦
    • 写在前面
    • 认识MVC
    • 解耦
      • 数据源
      • 代理
    • 创建cell的基础类
    • 创建section的相关类
    • 分离数据源
    • 分离代理
    • 总结
    • 参考资料

写在前面

最近在学习了解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;

其他可选的内容如下

- (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之中写一个扩展,添加需要的属性即可

// 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;
@endNS_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的设计更加合理,我们需要想办法来解决这些问题。

// 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;@endNS_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

分离数据源

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

//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;@endNS_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,然后在控制器之中回调出我们需要的进行的操作

//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);
@endNS_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需要进行判断,更加方便且易于维护。

#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:section1ItemsheaderTitle:@"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:section2ItemsheaderTitle:@"Section 2 Header"footerTitle:@"Section 2 Footer"];[self.dataSource addSection:section1];[self.dataSource addSection:section2];
}@end

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

image-20240828121956952

总结

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

参考资料

如何写好一个UITableView

UITableView 解耦

这篇关于「OC」初识MVC —— 简单学习UITableView的解耦的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122643

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直