Swift3 在UITableViewCell中显示带有按钮或UIImageview的UIViewController

Swift3 在UITableViewCell中显示带有按钮或UIImageview的UIViewController,swift3,tableview,Swift3,Tableview,我试图在用户单击表视图单元格中的按钮时,将其带到与当前单元格相关的新视图控制器 试试下面的代码 在cellForRowAt Indexath中执行此操作 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_Id

我试图在用户单击表视图单元格中的按钮时,将其带到与当前单元格相关的新视图控制器

试试下面的代码

在cellForRowAt Indexath中执行此操作

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_Identifier", for: indexPath) as! UITableViewCell
    cell.myButton.isUserInteractionEnabled = true
    cell.myButton.tag = indexPath.row
    cell.myButton.addTarget(self, action: #selector(didTapMyButton(_:)), for: UIControlEvents.touchUpInside)
    return cell
            }
这是按钮动作

func didTapMyButton(_ sender : UIButton) {
    let selectedIndex = sender.tag // You can get index here, so according to the index you can navigate to particular ViewController
    let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "UIViewController_identifier") as! UIViewController
    self.present(nextVC, animated: true, completion: nil)
}
希望能有帮助

检查此项:可能重复的