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

news2024/9/24 11:30:21

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

// 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

分离数据源

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

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

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

#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

image-20240828121956952

总结

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

参考资料

如何写好一个UITableView

UITableView 解耦

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2090228.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

VSCode连接SSH发生connection timeout

想用用实验室服务器连接vscode跑HM编码&#xff0c;今天突然连服务器连不到&#xff0c;报错&#xff1a;[13:47:13.068] Opening exec server for ssh-remotea406-server-lan [13:47:13.082] Initizing new exec server for ssh-remotea406-server-lan [13:47:13.089] Using c…

Apple LLM: 智能基础语言模型(AFM)

今天想和大家分享一下我最近在arXiv.org上看到苹果发表的一篇技术论文 Apple Intelligence Foundation Language Models (https://arxiv.org/abs/2407.21075)&#xff0c;概述了他们的模型训练。这虽然出乎意料&#xff0c;但绝对是一个积极的惊喜&#xff01; 这篇论文有那么多…

Dubbo如何传递链路追踪id?

1.什么是链路追踪&#xff1f; 分布式链路追踪就是将一次分布式请求还原成调用链路&#xff0c;将一次分布式请求的调用情况集中展示&#xff0c;比如各个服务节点上的耗时、请求具体到达哪台机器上、每个服务节点的请求状态等等。 链路跟踪主要功能&#xff1a; 故障快速定…

ET6框架(五)ECS组件式编程

文章目录 一、什么是ECS:二、ECS编程原则&#xff1a;三、组件生命周期&#xff1a; 一、什么是ECS: ECS即Entity、Component、System的简称&#xff0c;遵循组合优于继承的原则 Entity(实体) : 实体是一个概念&#xff0c;是指存在你游戏世界中的一个独立物体&#xff0c;是一…

图机器学习实战:从数据源到可视化

《动手学图机器学习》并不是一本纯粹介绍图机器学习理论的著作&#xff0c;Alessandro Negro 博士作为科学家和 Reco4 公司的 CEO&#xff0c;长期维护图数据源的推荐系统。他结合机器学习工程和图机器学习方法&#xff0c;通过推荐引擎、欺诈检测和知识图谱等案例&#xff0c;…

【书生2.4】InternLM + LlamaIndex RAG 实践

InternLM LlamaIndex RAG 实践 1 环境安装2 材料准备3 还原实验4 更换问题验证rag 【Intern Studio的gpu不足。本实验使用自有服务器】 1 环境安装 conda create -n llamaindex python3.10 conda activate llamaindex conda install pytorch2.0.1 torchvision0.15.2 torchau…

新160个crackme - 042-crackme

运行分析 提示需要key.dat文件 同目录下创建一个key.dat&#xff0c;提示key.dat内容不对 PE分析 C程序&#xff0c;32位&#xff0c;无壳 静态分析&动态调试 ida搜索字符串&#xff0c;找到关键字符串双击进入关键函数 对关键函数进行分析&#xff0c;注释如上当满足67行公…

信息安全数学基础(4)最大公因数

前言 在信息安全数学基础中&#xff0c;最大公因数&#xff08;Greatest Common Divisor, GCD&#xff09;是一个核心概念&#xff0c;它在密码学、数论等多个领域都有广泛应用。以下是对最大公因数的详细阐述&#xff1a; 一、定义 设a和b是两个非零整数&#xff0c;若整数d同…

论文阅读——Compact Single-Feed Dual-Mode Antenna for Active RFID Tag Application

文章目录 摘要一、天线设计A. 天线结构B. 天线演进 二、天线仿真与测试总结 论文来源&#xff1a;https://ieeexplore.ieee.org/document/7247651 摘要 文章提出了一种用于主动射频识别&#xff08;RFID&#xff09;标签应用的紧凑型单馈双模天线。该天线由一个位于FR4基板上的…

【读点论文】Scene Text Detection and Recognition: The Deep Learning Era

Scene Text Detection and Recognition: The Deep Learning Era Abstract 随着深度学习的兴起和发展&#xff0c;计算机视觉发生了巨大的变革和重塑。场景文本检测与识别作为计算机视觉领域的一个重要研究领域&#xff0c;不可避免地受到了这波革命的影响&#xff0c;从而进入…

ARM32开发——(二十一)ADC系统工作原理

1. ADC硬件结构 内部结构简化框图 2. ADC转换模式 2.1 单次转换,非扫描模式 2.2 连续转换,非扫描模式 2.3 单次转换,扫描模式 2.4 连续转换,扫描模式 3. 规则组和注入组 4. ADC数据对齐 5. ADC转换时间 总转换时间采样时间12 个 CK_ADC 周期 6. ADC内部校准 ADC有一个内置自…

前端速通面经八股系列(六)—— Vue(下)

接上ueue Vue下篇 四、路由1. Vue-Router 的懒加载如何实现2. 路由的hash和history模式的区别1. hash模式2. history模式3. 两种模式对比 3. 如何获取页面的hash变化4. $route 和$router 的区别5. 如何定义动态路由&#xff1f;如何获取传过来的动态参数&#xff1f;6. Vue-rou…

读书笔记:《深入理解Java虚拟机》(2)

Java内存区域与内存溢出异常 Java与C之间有一堵由内存动态分配和垃圾收集技术所围成的高墙&#xff0c;墙外面的人想进去&#xff0c;墙里 面的人却想出来。 对于从事C、C程序开发的开发人员来说&#xff0c;在内存管理领域&#xff0c;他们既是拥有最高权力的“皇帝”&#xf…

[Leetcode 435][Medium]-无重叠区间-贪心

目录 一、题目描述 二、整体思路 三、代码 一、题目描述 题目地址 二、整体思路 要求移除区间的最小个数&#xff0c;也就是要求使得剩余区间互不重叠的最大区间数。 可以先将区间按右端点的值进行升序排序,然后遍历数组&#xff0c;在此基础上保留左端点>最小右端点的值…

【C++ Primer Plus习题】7.7

问题: 解答: #include <iostream> using namespace std;#define SIZE 10double* fill_array(double* begin, double* end) {for (begin; begin < end; begin){cout << "请输入值:";cin >> *begin;if (cin.fail()){cout << "非法数字…

PCIE-校验和纠错

各种编码&#xff1a; &#xff08;1&#xff09;ECRC/LCRC (为什么需要ECRC?) &#xff08;3&#xff09;PCRC&#xff08;Plaintext CRC&#xff0c;纯文本 CRC&#xff09;&#xff1a;是 IDE TLP 中的概念&#xff0c;在大多数方面&#xff0c;PCRC的计算方法与ECRC相…

【论文阅读】为大规模航空图像应用神经辐射场

【论文阅读】为大规模航空图像应用神经辐射场 ABSTRACTI. INTRODUCTIONV. EXPERIMENTSA. Evaluations on the entire datasetsA.1. State-of-the-Art comparisonA.2. Cloud-to-Cloud comparisonA.3. Accuracy and completeness B. Evaluations on the selected regionsB.1. Fi…

时序预测相关论文阅读笔记

笔记链接&#xff1a; 【有道云笔记】读论文&#xff08;记录&#xff09;https://note.youdao.com/s/52ugLbot用于个人学习记录。

Python-MNE-源空间和正模型07:修复BEM和头表面

有时在创建BEM模型时&#xff0c;由于可能出现的一系列问题(例如&#xff0c;表面之间的交叉)&#xff0c;表面需要手动校正。在这里&#xff0c;我们将看到如何通过将表面导出到3D建模程序blender&#xff0c;编辑它们&#xff0c;并重新导入它们来实现这一点。我们还将给出一…

峟思大坝安全监测系统:科技筑牢工程稳定的基石

在全球工程建设的宏伟蓝图中&#xff0c;大坝安全始终占据着举足轻重的地位。随着科技的日新月异&#xff0c;大坝安全监测系统作为守护工程稳定的“哨兵”&#xff0c;正步入一个前所未有的创新与发展阶段。这一系统不仅是大坝健康管理的关键工具&#xff0c;更是确保民生安全…