// // GeneralTableViewController.swift // MTP2_iOS // // Created by Muchinfo on 2018/8/27. // Copyright © 2018年 Muchinfo. All rights reserved. // import UIKit class GeneralTableViewController: UITableViewController { fileprivate let identifier = "GeneralTableViewControllerCell" fileprivate let cancel = 100 fileprivate let done = 200 var data = [String]() typealias GeneralTableViewControllerCallback = (_ rowNum: Int, _ value: String) -> Void // 块定义 var callback: GeneralTableViewControllerCallback? // 持有 override func viewDidLoad() { super.viewDidLoad() } func didSelected(callback: @escaping GeneralTableViewControllerCallback) { self.callback = callback } // MARK: - Table view data source override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "GeneralTableViewControllerCell", for: indexPath) cell.textLabel?.text = data[indexPath.row] return cell } @IBAction func onClick(_ sender: UIBarButtonItem) { switch sender.tag { case cancel: self.dismiss(animated: true, completion: nil) case done: self.dismiss(animated: true) { if let callback = self.callback, let indexPath = self.tableView.indexPathForSelectedRow { callback(indexPath.row, self.data[indexPath.row]) } } default: break } } }