Skip to content

Instantly share code, notes, and snippets.

@zeitschlag
Last active March 9, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeitschlag/d44bc4f2d8bc7524b30614fd6d354ba9 to your computer and use it in GitHub Desktop.
Save zeitschlag/d44bc4f2d8bc7524b30614fd6d354ba9 to your computer and use it in GitHub Desktop.
import UIKit
class TableViewController: UITableViewController {
var elements : [String] = ["A", "B", "C"]
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(TableViewController.showAlert(_:)), name:NSNotification.Name(rawValue: "showAlert"), object: nil)
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.textLabel?.text = elements[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "showAlert")))
}
//MARK: - Notifications
func showAlert(_ notification: Notification) {
let alertController = UIAlertController(title: "Reset Seen Questions", message: "Are you sure you want to reset all questions to unseen?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Reset", style: .default, handler:{(action:UIAlertAction) -> Void in
// fake synchronous database-fetch
self.elements = ["1", "2", "3", "4"]
self.reloadData()
})
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
func reloadData() {
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment