GeneralTableViewController.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // GeneralTableViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2018/8/27.
  6. // Copyright © 2018年 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. class GeneralTableViewController: UITableViewController {
  10. fileprivate let identifier = "GeneralTableViewControllerCell"
  11. fileprivate let cancel = 100
  12. fileprivate let done = 200
  13. var data = [String]()
  14. typealias GeneralTableViewControllerCallback = (_ rowNum: Int, _ value: String) -> Void // 块定义
  15. var callback: GeneralTableViewControllerCallback? // 持有
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. }
  19. func didSelected(callback: @escaping GeneralTableViewControllerCallback) {
  20. self.callback = callback
  21. }
  22. // MARK: - Table view data source
  23. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  24. return data.count
  25. }
  26. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  27. let cell = tableView.dequeueReusableCell(withIdentifier: "GeneralTableViewControllerCell", for: indexPath)
  28. cell.textLabel?.text = data[indexPath.row]
  29. return cell
  30. }
  31. @IBAction func onClick(_ sender: UIBarButtonItem) {
  32. switch sender.tag {
  33. case cancel:
  34. self.dismiss(animated: true, completion: nil)
  35. case done:
  36. self.dismiss(animated: true) {
  37. if let callback = self.callback,
  38. let indexPath = self.tableView.indexPathForSelectedRow {
  39. callback(indexPath.row, self.data[indexPath.row])
  40. }
  41. }
  42. default:
  43. break
  44. }
  45. }
  46. }