Created
September 15, 2015 00:58
-
-
Save josephlord/af63ecaada47f2f2ab58 to your computer and use it in GitHub Desktop.
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, 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