如何通过表格视图单元格中的按钮锁定到新的视图控制器中

时间:2019-06-06 23:17:14

标签: ios swift tableview segue

如何通过表格视图单元格中的按钮来选择新的视图控制器?我需要发送一些数据,所以不确定我是否可以从tableViewCell类中完成该操作。

2 个答案:

答案 0 :(得分:1)

尝试

var profilePressed: ((UITableViewCell) -> Void)?

@IBAction func profileNamePressed(_ sender: Any) {
    profilePressed?(self)
}

在表格视图单元格类中,以及此:

cell.profilePressed = { (cell) in
    let profileVC = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC") as! ProfileVC
    profileVC.initData(withPostedBy: message.postedBy)
    self.presentDetail(profileVC)
}

在单元格中的索引路径功能处排

答案 1 :(得分:0)

像这样构造自定义单元格

class MyCell:UITableViewCell { 

  weak var delegate:VCName? 

  @IBAction func btnClicked(_ sender:UIButton) {
     delegate?.performSegue(withIdentifier:"segue",sender:someValue)
  }

}

在vc的cellForRowAt

let cell = ///
cell.delegate = self