Skip to content

Instantly share code, notes, and snippets.

@mlabraca
Created December 4, 2015 17:12
Show Gist options
  • Select an option

  • Save mlabraca/304a7a27dc09898cfa45 to your computer and use it in GitHub Desktop.

Select an option

Save mlabraca/304a7a27dc09898cfa45 to your computer and use it in GitHub Desktop.
TableView swift playground
import UIKit
import XCPlayground
//For more info, visit: http://blog.human-friendly.com/swift-2-xcode-7-gm-at-least-generic-support-for-at-objc-protocols
func reloadTableView(tableView: UITableView, dataSource: GenericTVCDatasource<String>, data: [String]) -> UITableView {
dataSource.reload(data)
tableView.reloadData()
return tableView
}
//class GenericTVCDelegate<A>: NSObject, UITableViewDelegate {
class GenericTVCDelegate: NSObject, UITableViewDelegate {
@objc func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("selected row: \(indexPath.row)")
}
}
class GenericTVCDatasource<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 reload(data: [A]) {
self.data = data
}
}
func simpleCellCreator(s: String) -> UITableViewCell {
let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "text")
//let cell = UITableViewCell(frame: CGRectZero)
cell.textLabel?.text = s
cell.detailTextLabel?.text = "test"
cell.detailTextLabel?.textColor = UIColor.redColor()
return cell
}
var strings = ["ba", "da", "bing"]
let datasource = GenericTVCDatasource(cellCreator: simpleCellCreator, data: strings)
let delegate = GenericTVCDelegate()
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 200, height: 400), style: .Plain)
tableView.dataSource = datasource
tableView.delegate = delegate
reloadTableView(tableView, dataSource: datasource, data: strings)
strings.append("more")
strings.append("one more")
reloadTableView(tableView, dataSource: datasource, data: strings)
//XCPShowView("Playground VC", view: tableView)
//XCPlaygroundPage.captureValue(tableView)
let view = UIView(frame: CGRectMake(0, 0, 200, 100))
view.backgroundColor = UIColor.redColor()
//Choose View > Assistant Editor > Show Assistant Editor
XCPlaygroundPage.currentPage.captureValue(view, withIdentifier: "red view")
XCPlaygroundPage.currentPage.captureValue(view.backgroundColor, withIdentifier: "red color")
XCPlaygroundPage.currentPage.captureValue(view.frame, withIdentifier: "frame view")
XCPlaygroundPage.currentPage.captureValue(tableView, withIdentifier: "tableView")
//XCPlaygroundPage.currentPage.liveView = tableView
//XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
@phynet
Copy link
Copy Markdown

phynet commented Jul 12, 2016

You missed import XCPlayground

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