Skip to content

Instantly share code, notes, and snippets.

@zonble
Created January 17, 2017 14:22
Show Gist options
  • Save zonble/3158bc5d03e557b014406778ba589afa to your computer and use it in GitHub Desktop.
Save zonble/3158bc5d03e557b014406778ba589afa to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
class SampleTableViewController : UITableViewController {
var selectedIndexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
if let selectedIndexPath = selectedIndexPath {
cell.accessoryType = indexPath == selectedIndexPath ? .checkmark : .none
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndexPath = indexPath
tableView.deselectRow(at: indexPath, animated: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
tableView.reloadData()
}
}
}
PlaygroundPage.current.liveView = SampleTableViewController(style: .plain)
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment