Created
August 4, 2014 09:24
-
-
Save ksmandersen/4d3530eeacd9c41786e5 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) | |
} | |
} | |
} |
@ksmandersen Thank you for the great example. It's very helpful for me. I'm just wondering if we get rid of the DataSourceProxy
class and make the ArrayDataSource
is the dataSource for the table/collectionView. would it cause any problems?
public class ArrayDataSource<CellType: UIView, ItemType>: NSObject, UITableViewDataSource, UICollectionViewDataSource {
private var items: [ItemType]
private var cellReuseIdentifier: String
private var configureClosure: (CellType, ItemType) -> Void
// private var proxy: UITableViewDataSource!
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
super.init()
self.configureDataSource()
}
private func configureDataSource() {
assert(view is UITableView || view is UICollectionView, "Passed view must be of type UITableView or UICollectionView")
// self.proxy = DataSourceProxy<CellType,ItemType>(self)
if let tableView = view as? UITableView {
tableView.dataSource = self
}
if let collectionView = view as? UICollectionView {
collectionView.dataSource = self
}
}
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)
}
@objc public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if items.count <= 0 {
return 0
}
return 1
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let count = items.count
return count
}
public 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
public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
@objc public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
if items.count <= 0 {
return 0
}
return 1
}
public 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
Thank you so much for this example.