Created
February 15, 2016 15:17
-
-
Save ptsakyrellis/0bdd8a881943ab2b126a to your computer and use it in GitHub Desktop.
Swift Class aimed to simplify the implementation of datasource methods for table or collection views where the data is an array.
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
/** | |
Class aimed to simplify the implementation of datasource methods for table or collection | |
views where the data is an array. | |
This allows us to re-use code and slim our view Controllers. | |
*/ | |
import Foundation | |
import UIKit | |
class ArrayDataSource<CellType: UIView, ItemType>: NSObject, UITableViewDataSource, UICollectionViewDataSource { | |
var items: [ItemType] | |
var cellReuseIdentifier: String | |
var configureClosure: (CellType, ItemType) -> Void | |
// MARK: Common methods | |
/// Setup the data sources with the items array, the cell identifier and the closure to configure the cells | |
init(items: [ItemType], cellReuseIdentifier: String, configureClosure: (CellType, ItemType) -> Void) { | |
self.items = items | |
self.cellReuseIdentifier = cellReuseIdentifier | |
self.configureClosure = configureClosure | |
super.init() | |
} | |
/// Returns the item from the array at a given index path | |
func itemAtIndexPath(indexPath: NSIndexPath) -> ItemType { | |
return self.items[indexPath.row] as ItemType | |
} | |
/// Select the item in the array and call the configure closure | |
func configureCell(cell: CellType, atIndexPath indexPath:NSIndexPath) { | |
let item = itemAtIndexPath(indexPath) | |
self.configureClosure(cell, item) | |
} | |
// MARK: UITableViewDataSource methods | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
if items.count <= 0 { | |
return 0 | |
} | |
return 1 | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.items.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellReuseIdentifier, forIndexPath: indexPath) as! CellType | |
configureCell(cell, atIndexPath: indexPath) | |
return cell as! UITableViewCell | |
} | |
// MARK: - UICollectionViewDataSource methods | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return items.count | |
} | |
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { | |
if items.count <= 0 { | |
return 0 | |
} | |
return 1 | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as! CellType | |
configureCell(cell, atIndexPath: indexPath) | |
return cell as! UICollectionViewCell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment