OC-MVC模式下的折叠cell
前言
在暑期的学习任务中已经使用过折叠cell,这里我在讲解一下如何在MVC架构模式下实现折叠cell
首先对于MVC模式 简单来说就是将程序分为三层,分别为Controller、Model、View,三者各司其职,Controller层负责协调Model与View层,处理大部分交互逻辑(将Model层的数据传送给view层展示出来,同时将View层的交互传到Model层以改变数据);Model层主要负责处理数据以及部分业务逻辑;View层负责数据的展示与数据的捕捉
引用学长的这张图片以更好的理解,我就不在赘述了
折叠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的大小,不在赘述