Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cellular/modeldatasource-swift
Handle cells & content for UITableViews and UICollectionViews
https://github.com/cellular/modeldatasource-swift
cocoapods datasource ios swift tvos uicollectionview uitableview
Last synced: about 2 months ago
JSON representation
Handle cells & content for UITableViews and UICollectionViews
- Host: GitHub
- URL: https://github.com/cellular/modeldatasource-swift
- Owner: cellular
- License: mit
- Created: 2018-09-21T07:56:55.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-07T05:04:17.000Z (over 2 years ago)
- Last Synced: 2024-09-15T13:28:05.321Z (3 months ago)
- Topics: cocoapods, datasource, ios, swift, tvos, uicollectionview, uitableview
- Language: Swift
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 9
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
modeldatasource
Easily and safely manage your content for data compatible views. The module is capable of handling two dimensional sets of data and simplifies displaying content in section and row/item based views.
Every set of data/content and the associated view is stored within a ModelCollection. The ModelCollection inherites from MutableCollection, RandomAccessCollection, RangeReplaceableCollection. Concrete classes conforming against ModelCollection are TableViewDataSource and CollectionViewDataSource.## Example TableViewDataSource
---```swift
// 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] = (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 = .init(model: firstHeaderModel, view: DecorativeView.self)
var firstSection: ModelSection = .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 = .init(model: secondHeaderModel, view: DecorativeView.self)
var secondSection: ModelSection = .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()```
## Cusomization
---
The default classes conforming against ModelCollection are marked as final and thus cannot be subclassed. Reason being that calling mutating functions (defined in ModelCollection) within the subclass was causing compiler errors. However there are still a few options for customizations.### 1. Extension on ModelCollection
```swift
// 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. Decorate an existing datasorce
```swift
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. Full Custom Code
* Create a custom class / struct and implement every function required to conform against ModelCollection.
* Make sure that you resolve all associated types.
* Store your data somewhere (e.g. Array, TableViewDataSource.Buffer)## Requirements
---
* iOS 10.3+
* tvOS 10.2+## Installation
---```ruby
### Cocoapods
pod 'CellularModelDataSource'
```