modeldatasource
轻松并安全地管理数据兼容视图的内容。该模块能够处理二维数据集,并简化了基于分区和行/项视图的内容显示。每组数据/内容和关联的视图都存储在ModelCollection中。ModelCollection继承自MutableCollection、RandomAccessCollection、RangeReplaceableCollection。遵守ModelCollection的具体类是TableViewDataSource和CollectionViewDataSource。
示例TableViewDataSource
// The Example shows how to display two sections, each with 10 cells and a header.
//
// To use a data source there a certain requirements that have to be fulfilled:
// Every cell or header/footer view needs to conform against ModelDataSourceViewDisplayable.
// They can be stored with their associated content (Model) in either a ModelItem or ModelDecorative
// which are contstrained to a specific view conforming against ModelDataSourceView (e.g. UITableView).
// Each section is represented by a ModelSection holding ModelItems and ModelDecoratives.
//
// NOTE: The Example folder contains the full code.
var dataSource = TableViewDataSource()
// Setup the UITableViewView
let tableView = UITableView()
tableView.register(TableViewCell.self)
tableView.register(DecorativeView.self)
tableView.dataSource = dataSource
// Items to be displayed in each section-
let items: [ModelItem<UITableView>] = (1...10).map { index in
return .init(model: .init(color: .gray, title: "cell # \(index)"), cell: TableViewCell.self)
}
// Build first section
let firstHeaderModel: DecorativeView.Model = .init(color: .darkGray, title: "Header 1")
let firstDecorative: ModelDecorative<UITableView> = .init(model: firstHeaderModel, view: DecorativeView.self)
var firstSection: ModelSection<UITableView> = .init(decorative: firstDecorative, kind: .header)
firstSection.append(contentsOf: items)
// Build second section.
let secondHeaderModel: DecorativeView.Model = .init(color: .darkGray, title: "Header 2")
let secondDecorative: ModelDecorative<UITableView> = .init(model: secondHeaderModel, view: DecorativeView.self)
var secondSection: ModelSection<UITableView> = .init(decorative: secondDecorative, kind: .header)
secondSection.append(contentsOf: items)
// Append the sections to the TableViewDataSource.
dataSource.append(contentsOf: [firstSection, secondSection])
// Load the content into the UITableView.
tableView.reloadData()
自定义
遵守ModelCollection的默认类被标记为final,因此不能被继承。原因是:在子类中调用在ModelCollection中定义的生效函数会导致编译器错误。尽管如此,仍然有一些自定义选项。
1. 模型收藏集的扩展
// Extension on all ModelCollections regardless of the DataSourceView.
extension ModelCollection {
func someCustomCode() { ... }
mutating func someCustomMutatingCode() { ... }
}
// Extension bound to all ModelCollections managing UITableViews.
extension ModelCollection where DataSourceView == UITableView {
func someCustomTableViewCode() { ... }
mutating func someCustomMutatingTableViewCode() { ... }
}
2. 装饰现有的数据源
final class CustomDataSource: ModelCollection {
typealias DataSourceView = CollectionViewDataSource.DataSourceView
typealias Index = CollectionViewDataSource.Index
private var dataSource: CollectionViewDatasource
// MARK: - ModelCollection
init() {
dataSource = .init()
}
var startIndex: Index {
return dataSource.startIndex
}
var endIndex: Index {
return dataSource.endIndex
}
// .....
// MARK: Custom code
func someCustomCode() { ... }
}
3. 完全自定义代码
- 创建一个自定义类/结构体,并实现所有符合模型收藏集要求的函数。
- 确保解决所有相关类型。
- 将您数据存储在某个地方(例如:数组,TableViewDataSource.Buffer)。
需求
- iOS 10.3+
- tvOS 10.2+
安装
### Cocoapods
pod 'CellularModelDataSource'