Last active
August 29, 2015 14:04
-
-
Save ksmandersen/72349811f5100d35c8a2 to your computer and use it in GitHub Desktop.
Generic Array DataSource
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 ArrayDataSource<CellType: UIView, ItemType>: NSObject, UITableViewDataSource, UICollectionViewDataSource { | |
var items: [ItemType] | |
var cellReuseIdentifier: String | |
var configureClosure: (CellType, ItemType) -> Void | |
init(items: [ItemType], cellReuseIdentifier: String, configureClosure: (CellType, ItemType) -> Void) { | |
self.items = items | |
self.cellReuseIdentifier = cellReuseIdentifier | |
self.configureClosure = configureClosure | |
super.init() | |
} | |
func itemAtIndexPath(indexPath: NSIndexPath) -> ItemType { | |
return self.items[indexPath.row] as ItemType | |
} | |
func configureCell(cell: CellType, atIndexPath indexPath:NSIndexPath) { | |
let item = itemAtIndexPath(indexPath) | |
self.configureClosure(cell, item) | |
} | |
// MARK: UITableViewDataSource | |
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 | |
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! { | |
var 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