IRCollectionTableViewModel
- IRCollectionTableViewModel是一个强大的iOS中的MVVM Tableview/CollectionView,它灵活且易于处理和重用。
特性
- MVVM结构。
- 灵活,可重用。
安装
Git
- 克隆此项目。
- 将此项目复制到您的项目中。
- 将.xcodeproj文件添加到您的项目中,并将其链接为嵌入式框架。
选项
- 您可以移除
demo
和ScreenShots
文件夹。
Cocoapods
- 在
Podfile
中添加pod 'IRCollectionTableViewModel'
执行
pod install
介绍MVVM
- 模型--视图--视图模型(MVVM)是一种软件开发模式。
- 它比传统的MVC架构有更多优点。可以提高整体代码结构。
- 更多详细信息请参阅MVVM维基百科。[MVVM](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel)
使用
基本
UITableView
- 创建一个新的类
TableViewViewModel
,它扩展了TableViewBasicViewModel<UITableViewDataSource>
,并导入IRCollectionTableViewModel
#import <IRCollectionTableViewModel/IRCollectionTableViewModel.h>
@interface TableViewViewModel : TableViewBasicViewModel<UITableViewDataSource>
@end
- 您可以在内部添加您的初始化方法和注册cell
- (instancetype)initWithTableView:(UITableView*)tableView;
...
- (instancetype)initWithTableView:(UITableView *)tableView {
if (self = [super init]) {
items = [[NSMutableArray<id<SectionModelItem>> alloc] init];
[tableView registerNib:[UINib nibWithNibName:TableViewCell.identifier bundle:nil] forCellReuseIdentifier:TableViewCell.identifier];
}
return self;
}
- 添加
update
方法
- (void)update;
...
- (void)update {
[items removeAllObjects];
// Setup items
}
- 为了设置
items
,也就是设置您想要显示的部分/行。创建TableViewSectionItem
、TableViewRowItem
和TableViewSectionType
typedef NS_ENUM(NSInteger, ProfileRowType){
RowType_DemoRow
};
@interface TableViewRowItem : RowBasicModelItem
@property (readonly) ProfileRowType type;
@end
implementation TableViewRowItem
@dynamic type;
@end
@interface TableViewSectionItem : SectionBasicModelItem
@property (nonatomic) NSString* sectionTitle;
@property (nonatomic) SectionType type;
@end
@implementation TableViewSectionItem
@end
- 设置
items
- (void)setupRows {
NSMutableArray *rowItems = [NSMutableArray array];
[rowItems addObject:[[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"]];
[rowItems addObject:[[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"]];
NSArray *demoRowItems = [NSArray arrayWithArray:rowItems];
TableViewSectionItem *item = [[TableViewSectionItem alloc] initWithRowCount:[demoRowItems count]];
item.type = DemoSection;
item.sectionTitle = @"Demo Section";
item.rows = demoRowItems;
[items addObject:item];
}
- 重写
UITableViewDataSource
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return items.count;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [items[section] rowCount];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
id<SectionModelItem> item = [items objectAtIndex:indexPath.section];
TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
switch (item.type) {
case DemoSection:
{
switch (row.type) {
case RowType_DemoRow:
{
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableViewCell.identifier forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%@%ld", row.title, row.tagRange.location];
cell.editTextField.text = [editedTexts objectAtIndex:indexPath.row];
cell.editTextField.tag = row.tagRange.location;
cell.editTextField.delegate = self;
return cell;
}
}
break;
}
default:
break;
}
return [[UITableViewCell alloc] init];
}
- 使用您的视图模型
TableViewViewModel
#import "TableViewViewModel.h"
@implementation TableViewController {
TableViewViewModel *viewModel;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:TableViewHeaderView.identifier bundle:nil] forHeaderFooterViewReuseIdentifier:TableViewHeaderView.identifier];
viewModel = [[TableViewViewModel alloc] initWithTableView:_tableView];
_tableView.dataSource = viewModel;
[viewModel update];
}
@end
CollectionView
-
与
TableViewViewModel
相同的方式。创建一个新类CollectionBrowserViewModel
继承自TableViewBasicViewModel<UITableViewDataSource>
,并导入IRCollectionTableViewModel
-
您可以在内部添加您的初始化方法和注册cell
-
为设置
items
提供服务,即设置您想显示的分区/行。创建CollectionBrowserSectionItem
、CollectionBrowserRowItem
和CollectionBrowserSectionType
-
重写
UICollectionViewDataSource
高级设置
TableViewBasicViewModel方法
TableViewBasicViewModel
提供了一些使用方法
- (NSInteger)getRowTypeWith:(SectionType)type row:(NSInteger)row;
- (NSString *)getSectionTitleinSection:(NSInteger)section;
- (UIImage *)getSectionLeftIconinSection:(NSInteger)section;
- (SectionType)getSectionTypeinSection:(NSInteger)section;
- (void)hideRows:(BOOL)hide inSection:(NSInteger)section;
- (BOOL)hiddenRowsinSection:(NSInteger)section;
- (NSIndexSet *)getIndexSetWithSectionType:(SectionType)sectionType;
- (NSIndexPath *)getIndexPathWithSectionType:(SectionType)sectionType rowType:(RowType)rowType;
- (void)setupRowTag;
- (NSIndexPath *)getIndexPathFromRowTag:(NSInteger)rowTag;
标签
-
因为单元格具有重用功能,有时我们需要给单元格/组件标记,以便识别特定的单元格/组件,因此
IRCollectionTableViewModel
提供标签功能 -
通过
setupRowTag
设置标签,它将标签信息保存到tagRange
中,其中在RowBasicModelItem
中
- (void)setupRows {
...
[self setupRowTag];
}
- 获取标签
TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
tag = row.tagRange.location;
- 通过标签获取索引路径
- (NSIndexPath *)getIndexPathFromRowTag:(NSInteger)rowTag;
- 有时您想为UI组件如
UITextField
标记,使用setTagRangeLength
TableViewRowItem *row = [[TableViewRowItem alloc] initWithType:RowType_DemoRow withTitle:@"Demo Row"];
[row setTagRangeLength:2];
...
[self setupRowTag];
- 然后获取标签
TableViewRowItem *row = (TableViewRowItem *)[item.rows objectAtIndex:[indexPath row]];
tag1 = row.tagRange.location;
tag2 = row.tagRange.location + 1;
cell.textField1.tag = tag1;
cell.textField2.tag = tag2;
- 标签与相同的索引路径映射
[self getIndexPathFromRowTag:tag1] == [self getIndexPathFromRowTag:tag2]
现在,您可以轻松地为任何内容标记
获取行类型
- 通过
(void)hideRows:(BOOL)hide inSection:(NSInteger)section
隐藏特定分区的行
- (NSInteger)getRowTypeWith:(SectionType)type row:(NSInteger)row;
获取标题
- 获取在
SectionBasicModelItem
中设置的标题
- (NSString *)getSectionTitleinSection:(NSInteger)section;
获取左侧图标
- 获取在
SectionBasicModelItem
中设置的图标
- (UIImage *)getSectionLeftIconinSection:(NSInteger)section;
获取类型
- 通过分区索引获取类型
- (SectionType)getSectionTypeinSection:(NSInteger)section;
隐藏行
- 通过
(void)hideRows:(BOOL)hide inSection:(NSInteger)section
隐藏特定分区的行 - 通过
(BOOL)hiddenRowsinSection:(NSInteger)section
检查隐藏状态
- (void)hideRows:(BOOL)hide inSection:(NSInteger)section;
- (BOOL)hiddenRowsinSection:(NSInteger)section;
获取索引集合
- 通过类型获取分区索引
- (NSIndexSet *)getIndexSetWithSectionType:(SectionType)sectionType;
获取索引路径
- 通过类型和行类型获取索引路径
- (NSIndexPath *)getIndexPathWithSectionType:(SectionType)sectionType rowType:(RowType)rowType;
截图
表格视图 | 收集视图 |
---|---|
![]() |
![]() |