-
-
Save ytlvy/c905a2a6e17795f1e83d689b15108df8 to your computer and use it in GitHub Desktop.
Workaround for: 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
public class ArrayDataSource<CellType: UIView, ItemType> { | |
private var items: [ItemType] | |
private var cellReuseIdentifier: String | |
private var configureClosure: (CellType, ItemType) -> Void | |
private var proxy: DataSourceProxy! | |
private unowned var view: UIView | |
public init(view: UIView, items: [ItemType], cellReuseIdentifier: String, configureClosure: (CellType, ItemType) -> Void) { | |
self.items = items | |
self.cellReuseIdentifier = cellReuseIdentifier | |
self.configureClosure = configureClosure | |
self.view = view | |
configureDataSource() | |
} | |
private func configureDataSource() { | |
assert(view as? UITableView || view as? UICollectionView, "Passed view must be of type UITableView or UICollectionView") | |
self.proxy = DataSourceProxy(self) | |
if let tableView = view as? UITableView { | |
tableView.dataSource = proxy | |
} | |
if let collectionView = view as? UICollectionView { | |
collectionView.dataSource = proxy | |
} | |
} | |
public func itemAtIndexPath(indexPath: NSIndexPath) -> ItemType { | |
return self.items[indexPath.row] as ItemType | |
} | |
public func configureCell(cell: CellType, atIndexPath indexPath:NSIndexPath) { | |
let item = itemAtIndexPath(indexPath) | |
self.configureClosure(cell, item) | |
} | |
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 | |
} | |
private class DataSourceProxy: NSObject, UITableViewDataSource, UICollectionViewDataSource { | |
private unowned var dataSource: ArrayDataSource<UIView, Any> | |
init(_ dataSource: AnyObject) { | |
self.dataSource = dataSource as ArrayDataSource<UIView, Any> | |
} | |
// MARK: UITableViewDataSource | |
func numberOfSectionsInTableView(tableView: UITableView!) -> Int { | |
return dataSource.numberOfSectionsInTableView(tableView) | |
} | |
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { | |
return dataSource.tableView(tableView, numberOfRowsInSection: section) | |
} | |
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { | |
return dataSource.tableView(tableView, cellForRowAtIndexPath: indexPath) | |
} | |
// MARK: - UICollectionViewDataSource | |
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int { | |
return dataSource.collectionView(collectionView, numberOfItemsInSection: section) | |
} | |
func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int { | |
return dataSource.numberOfSectionsInCollectionView(collectionView) | |
} | |
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! { | |
return dataSource.collectionView(collectionView, cellForItemAtIndexPath: indexPath) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment