Skip to content

Instantly share code, notes, and snippets.

@josephlord
Created September 15, 2015 10:00
Show Gist options
  • Save josephlord/5089019943b3667e5516 to your computer and use it in GitHub Desktop.
Save josephlord/5089019943b3667e5516 to your computer and use it in GitHub Desktop.
Fails with "Non objc-method numberOfTableCells cannot satisy optional requirement of @objc protocol UITableViewDatasource and other similar errors when the delegate implementation is in the extension. See: https://gist.github.com/josephlord/af63ecaada47f2f2ab58 for a non-extension working version of this code.
import UIKit
class GenericTVCDelegate<A> : NSObject {
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
@nezhyborets
Copy link

Hi! Any news on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment