ItemsDataSource
ItemsDataSource is a generic datasource for UICollectionView.
Features
- Create collections with any data types
- Reusable UICollectionViewCell and UICollectionReusableView
- UICollectionView at its core
- Easy extendable
Requirements
- iOS 10.0+
- Xcode 9.0+
- Swift 5.0+
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsCocoaPods 1.1+ is required to build ItemsDataSource.
To integrate ItemsDataSource into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'ItemsDataSource'
endif you're using CocoaPods 1.5.0+ you can include ItemsDataSource as static library:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
target '<Your Target Name>' do
pod 'ItemsDataSource'
endThen, run the following command:
$ pod installCarthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthageTo integrate ItemsDataSource into your Xcode project using Carthage, specify it in your Cartfile:
github "ItemsDataSource"
Run carthage update to build the framework and drag the built ItemsDataSource.framework into your Xcode project.
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but ItemsDataSource does support its use on supported platforms.
Once you have your Swift package set up, adding ItemsDataSource as a dependency is as easy as adding it to the dependencies value of your Package.swift.
Swift 4
dependencies: [
.package(url: "https://github.com/minikin/ItemsDataSource.git")
]Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate ItemsDataSource into your project manually.
Just copy files from Sources folder to your projects:
CellDescriptor.swift
Groupable.swift
Itemable.swift
ItemsDataSource.swift
SupplementaryDescriptor.swiftUsage
Define your model as usual
import UIKit
import ItemsDataSource
struct Vitamin {
// MARK: - Instance Properties
let name: String
let amount: Double
}Conform your model to Itemable
extension Vitamin: Itemable {
var itemCellDescriptor: CellDescriptor {
return CellDescriptor(reuseIdentifier: ReuseIdentifier.vitaminCell, configure: configureIngredientCell)
}
}
Add configure method to model in extension
extension Vitamin {
func configureIngredientCell(_ cell: ViataminCell) {
cell.vitaminNameLabel.text = name
cell.backgroundColor = UIColor.randomColor()
}
}In ViewController inject your datasource
import ItemsDataSource
import UIKit
final class ExampleViewController: UIViewController {
// MARK: - Injections
public var vitaminsDataSourse = ItemsDataSource(items: [Vitamin](),
cellDescriptor: { $0.itemCellDescriptor })
// MARK: - IBOutlets
@IBOutlet var exampleCollectionView: UICollectionView! {
didSet {
setExampleCollectionViewDataSource()
exampleCollectionView.delegate = self
setExampleCollectionViewLayout()
exampleCollectionView.reloadData()
}
}
// MARK: - Instance Properties
var vitamins = [Vitamin]()
// MARK: - ViewController LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
print("vitamins", vitamins)
}
// MARK: - Helpers
private func setExampleCollectionViewDataSource() {
vitaminsDataSourse.items = vitamins
exampleCollectionView.dataSource = vitaminsDataSourse
}
private func setExampleCollectionViewLayout() {
let layout = CommonFlowLayout(columns: 2,
itemHeight: 200,
inset: 5,
spacing: 0,
lineSpacing: 5)
exampleCollectionView.collectionViewLayout = layout
}
}
// MARK: - UICollectionViewDelegate
extension ExampleViewController: UICollectionViewDelegate {}For more details, please check an iOS example or ask me on twitter: @minikin
License
ItemsDataSource is released under the MIT license. See LICENSE for details.
