SCTBDataSource
SCTBDataSource
将 UITableView
的 dataSource 和 delegate 分离开来。您不必再次编写一些常用的 dataSource 和 delegate 函数。您只需关注组合数据和自定义 UITableViewCell
。
安装
CocoaPods
安装 SCTBDataSource 最简单的方法是通过 CocoaPods
pod 'SCTBDataSource'
传统方法
- 下载 SCTBDataSource 项目。
- 将 Core 文件夹拖到您的项目中。
描述
SCTBRowItem
SCTBRowItem
包含创建 UITableViewCell
非常多的必要属性。
@property (nonatomic, strong) id rowData;
@property (nonatomic, assign) CGFloat rowHeight;
@property (nonatomic, copy) NSString *cellClassString;
注意
必须为 cellClassString
属性设置自定义 UITableViewCell
的类名。如果没有正确设置属性,SCTBDataSource
将使用系统默认样式创建 UItableViewCell
。
SCTBSectionItem
SCTBSectionItem
是一个对象,它描述了 UITableView
的某些区的信息。如果你的整个区域的 UITableViewCell
都相同,你可以将名为 rowItemIsSameOne
的属性的值设置为 YES
。然后设置正确的 sectionRowHeight
和 sectionCellClassString
值。
注意
当 rowItemIsSameOne
的值为 NO
时,sectionRowHeight
和 sectionCellClassString
无效。
SCTBDataSource
SCTBDataSource
遵循 UITableViewDataSource
,并实现必要的协议函数。您需要将包含 SCTBSectionItem
的可变数组传递给对象,然后它将根据区域项信息创建 UITableViewCell
。
SCTBDelegate
SCTBDelegate
遵循 UITableViewDelegate
,并实现了某些协议函数。如果你想将一些事件传递给父视图,可以为特定的协议函数设计块。
用法
- 创建
SCTBDataSource
和SCTBDelegate
对象如下
NSMutableArray<SCTBRowItem *> *rowItems = [NSMutableArray array];
for (NSString *data in self.datas) {
// system default style UITableViewCell
SCTBRowItem *rowItem = [SCTBRowItem rowItemWithRowData:data];
rowItem.rowHeight = 100.f;
[rowItems addObject:rowItem];
}
SCTBSectionItem *sectionItem = [SCTBSectionItem sectionItemWithRowItems:rowItems];
NSMutableArray<SCTBSectionItem *> *sectionItems = [NSMutableArray array];
[sectionItems addObject:sectionItem];
if (!self.dataSource) {
self.dataSource = [SCTBDataSource dataSourceWithSectionItems:sectionItems];
} else {
self.dataSource.sectionItems = sectionItems;
}
if (!self.delegate) {
self.delegate = [SCTBDelegate delegatWithSectionItems:sectionItems];
} else {
self.delegate.sectionItems = sectionItems;
}
self.tableView.dataSource = self.dataSource;
self.tableView.delegate = self.delegate;
注意
如果创建系统默认样式的 UITableViewCell
,应该为 UITableViewCell
创建分类,并符合名为 SCTBDataSourceProtocol
的协议,然后在需要设置子视图值时实现 setupTableViewCellWithRowData
函数。如果您创建自定义的 UITableViewCell
,则自定义单元格应该符合 SCTBDataSourceProtocol
并实现协议函数。
其他详细信息,请参阅 Example
文件夹。
版权信息
SCTBDataSource
根据 MIT 许可证条款授权。