Created
September 15, 2015 10:00
-
-
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.
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Any news on this?