Skip to content

Instantly share code, notes, and snippets.

@josephlord
Created September 15, 2015 00:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephlord/af63ecaada47f2f2ab58 to your computer and use it in GitHub Desktop.
Save josephlord/af63ecaada47f2f2ab58 to your computer and use it in GitHub Desktop.
import UIKit
class GenericTVCDelegate<A> : NSObject, UITableViewDataSource {
var cellCreator:A->UITableViewCell
var data:[A]
init(cellCreator:A->UITableViewCell, data:[A]) {
self.cellCreator = cellCreator
self.data = data
}
//}
//extension GenericTVCDelegate : UITableViewDataSource {
@objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cellCreator(data[indexPath.row])
}
@objc func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
@objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
}
func simpleCellCreator(s:String)->UITableViewCell {
let cell = UITableViewCell(frame: CGRectZero)
cell.textLabel?.text = s
return cell
}
let strings = ["ba", "da", "bing"]
let datasource = GenericTVCDelegate(cellCreator: simpleCellCreator, data: strings)
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 200, height: 400), style: .Plain)
tableView.dataSource = datasource
tableView.reloadData()
tableView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment