如何在 UITableView 中获取选定的项目

How can I get selected item in UITableView

如何在 Swift3 中从 UITableView 中获取选定的项目? 我这样试试:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    //do something
}

但这对我不起作用。

首先,您必须将 @IBOutlet 设置为 ViewController 中的 tableView 并设置为 delegatedataSource 以便您可以看到数据响应 tableView ;

中的变化
class YourViewController : UIViewController, UITableViewDelegate, UITableViewDataSource

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

之后

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    }

您可以获取选定的单元格。 Ty.