当前位置: 首页 > news >正文

OC-MVC模式下的折叠cell

前言

在暑期的学习任务中已经使用过折叠cell,这里我在讲解一下如何在MVC架构模式下实现折叠cell

首先对于MVC模式 简单来说就是将程序分为三层,分别为Controller、Model、View,三者各司其职,Controller层负责协调Model与View层,处理大部分交互逻辑(将Model层的数据传送给view层展示出来,同时将View层的交互传到Model层以改变数据);Model层主要负责处理数据以及部分业务逻辑;View层负责数据的展示与数据的捕捉

f14fbbd8f95f4b6f9ab7ed50cb64a9ed

引用学长的这张图片以更好的理解,我就不在赘述了

折叠cell实现

Model层

这里Model层主要存储了两个数据,一个是cell的折叠情况,一个是单元格内容

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
​
@interface Model : NSObject
@property (nonatomic, strong)NSMutableArray* array;
@property (nonatomic, assign)BOOL isExpand;
@end
​
NS_ASSUME_NONNULL_END
#import "Model.h"
@implementation Model
- (instancetype)init {if (self = [super init]) {self.array = [[NSMutableArray alloc] init];self.isExpand = NO;}return self;
}
@end#import "Model.h"
@implementation Model
- (instancetype)init {if (self = [super init]) {self.array = [[NSMutableArray alloc] init];self.isExpand = NO;}return self;
}
@end

View层

view层则是展示UI,这里面临着一个问题,是将UItableView协议方法交给谁来控制,这里有两个选择,一是直接交给View层来控制,只要把数据源给View层就行了,但是并不符合MVC设计模式,所以还是选择交给Controller来管理比较合适

#import <UIKit/UIKit.h>
​
NS_ASSUME_NONNULL_BEGIN
​
@interface View : UIView
@property (nonatomic, strong)UITableView* tableView;
​
@end
​
NS_ASSUME_NONNULL_END
​
#import "View.h"
#import "Masonry.h"
@implementation View
- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];self.backgroundColor = [UIColor systemGroupedBackgroundColor];self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];[self addSubview:self.tableView];[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {make.center.equalTo(self);make.height.width.mas_equalTo(100);}];return self;
}

Controller层

这里主要负责管理View层与Model层之间的信息交互,以及实现UItableView的协议方法

#import <UIKit/UIKit.h>
​
NS_ASSUME_NONNULL_BEGIN
​
@interface Controller : UIViewController
@end
​
NS_ASSUME_NONNULL_END#import "Controller.h"
#import "Masonry.h"
#import "View.h"
#import "Model.h"
@interface Controller ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)View* landView;
@property(nonatomic, strong)Model* landModel;
@end
​
@implementation Controller
​
- (void)viewDidLoad {[super viewDidLoad];self.landModel = [[Model alloc] init];self.landModel.array = [NSMutableArray arrayWithObjects:@"我", @"是", @"小", @"李", nil];self.landView = [[View alloc] initWithFrame:self.view.bounds];self.landView.tableView.delegate = self;self.landView.tableView.dataSource = self;[self.view addSubview:self.landView];
}
​
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.landModel.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell* cell = [self.landView.tableView dequeueReusableCellWithIdentifier:@"cell01"];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell01"];}cell.textLabel.text = self.landModel.array[indexPath.row];return cell;
}
​
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {NSString* str = self.landModel.array[indexPath.row];[self.landModel.array removeObjectAtIndex:indexPath.row];[self.landModel.array insertObject:str atIndex:0];[self.landView.tableView reloadData];[self pressButton];
}
​
- (void)pressButton{self.landModel.isExpand = !self.landModel.isExpand;if (self.landModel.isExpand) {[self.landView.tableView mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(400);}];} else {[self.landView.tableView mas_updateConstraints:^(MASConstraintMaker *make) {make.height.mas_equalTo(100);}];}
}

实现折叠cell的关键就是改变tableView的大小,不在赘述

http://www.dtcms.com/a/349616.html

相关文章:

  • 利用 Python 爬虫获取 1688 商品详情 API 返回值说明(代码示例)实战指南
  • 爬虫基础学习-爬取网页项目
  • vue2使用WaveSurfer实现简易的音频播放
  • 波音787项目:AR技术重塑航空制造的数字化转型
  • 用MessageBus优化模块通信:实现订阅/发布模式
  • nmcli命令详解
  • 文吃透朴素贝叶斯:从原理到实战
  • 【python文件处理】使用 open() 函数打开文件、 File 操作文件、使用 OS 对象操作文件目录的知识,使用 open() 函数打开文件
  • DMP-Net:面向脑组织术中成像的深度语义先验压缩光谱重建方法|文献速递-深度学习人工智能医疗图像
  • Android进入Activity时闪黑生命周期销毁并重建
  • 集成电路学习:什么是Caffe深度学习框架
  • 强化学习核心概念与算法详解-马尔可夫决策过程(MDP)+贝尔曼方程(Bellman Equation)
  • 合同管理软件的主要功能有什么?
  • 朴素贝叶斯学习笔记:从原理到实战(J享)
  • (LeetCode 每日一题) 498. 对角线遍历 (矩阵、模拟)
  • SSM从入门到实战:3.2 SpringMVC请求处理与控制器
  • 《C++哈希表:高效数据存储与检索的核心技术》
  • 朴素贝叶斯算法学习总结
  • MySQL 磁盘和 Redis 内存
  • 无人机航拍数据集|第22期 无人机城市交通目标检测YOLO数据集8624张yolov11/yolov8/yolov5可训练
  • Coze用户账号设置修改用户头像-前端源码
  • 【ACP】2025-最新-疑难题解析-5
  • Python Day 33 JavaScript BOM 与 DOM 核心笔记整合
  • 【数学建模】如何总结数学建模中的层次分析法最好
  • 通过Fiddler肆意修改接口返回数据进行测试
  • EXCEL自动调整列宽适应A4 A3 A2
  • OpenCV计算机视觉实战(21)——模板匹配详解
  • 将盾CDN:高防CDN和游戏盾有什么区别?
  • 宋红康 JVM 笔记 Day07|本地方法接口、本地方法栈
  • More Effective C++ 条款08:理解各种不同意义的new和delete