Last active
February 15, 2022 10:52
-
-
Save joshuadutton/bf674b70c708bc7e8522 to your computer and use it in GitHub Desktop.
Generic Collection View and Table View data sources in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol CollectionViewCellConfigurable { | |
typealias ItemType | |
typealias CellType: UICollectionViewCell | |
static func reuseIdentifierForIndexPath(indexPath: NSIndexPath) -> String | |
static func configureCellAtIndexPath(indexPath: NSIndexPath, item: ItemType, cell: CellType) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CollectionViewDataSource<T: IndexPathIndexable, C: CollectionViewCellConfigurable where T.ItemType == C.ItemType>: NSObject, UICollectionViewDataSource { | |
let data: T | |
init(data: T) { | |
self.data = data | |
} | |
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { | |
return data.numberOfSections() | |
} | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return data.numberOfItemsInSection(section) | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
let reuseIdentifier = C.reuseIdentifierForIndexPath(indexPath) | |
guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? C.CellType else { | |
fatalError("Cells with reuse identifier \(reuseIdentifier) not of type \(C.CellType.self)") | |
} | |
let item = data.objectAtIndexPath(indexPath) | |
C.configureCellAtIndexPath(indexPath, item: item, cell: cell) | |
return cell | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol IndexPathIndexable { | |
// associatedType keyword is comming in Swift 2.2 | |
typealias ItemType | |
func objectAtIndexPath(indexPath: NSIndexPath) -> ItemType | |
func numberOfSections() -> Int | |
func numberOfItemsInSection(section: Int) -> Int | |
} | |
extension Array: IndexPathIndexable { | |
func objectAtIndexPath(indexPath: NSIndexPath) -> Element { | |
return self[indexPath.item] | |
} | |
func numberOfSections() -> Int { | |
return 1 | |
} | |
func numberOfItemsInSection(section: Int) -> Int { | |
return count | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol TableViewCellConfigurable { | |
typealias ItemType | |
typealias CellType: UITableViewCell | |
static func reuseIdentifierForIndexPath(indexPath: NSIndexPath) -> String | |
static func configureCellAtIndexPath(indexPath: NSIndexPath, item: ItemType, cell: CellType) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class TableViewDataSource<T: IndexPathIndexable, C: TableViewCellConfigurable where T.ItemType == C.ItemType>: NSObject, UITableViewDataSource { | |
let data: T | |
init(data: T) { | |
self.data = data | |
} | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return data.numberOfSections() | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return data.numberOfItemsInSection(section) | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let reuseIdentifier = C.reuseIdentifierForIndexPath(indexPath) | |
guard let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? C.CellType else { | |
fatalError("Cells with reuse identifier \(reuseIdentifier) not of type \(C.CellType.self)") | |
} | |
let item = data.objectAtIndexPath(indexPath) | |
C.configureCellAtIndexPath(indexPath, item: item, cell: cell) | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You use it like this:
Your custom data model object:
In your view controller:
In your custom cell: