【问题标题】:How to segue into a new view controller from button in table view cell如何从表格视图单元格中的按钮进入新的视图控制器
【发布时间】:2019-06-06 23:17:14
【问题描述】:

如何从表格视图单元格中的按钮进入新的视图控制器?我需要用它发送一些数据,所以我不确定我是否可以从我的 tableViewCell 类中做到这一点。

【问题讨论】:

    标签: ios swift tableview segue


    【解决方案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)
    }
    

    在您的单元格中用于索引路径函数处的行

    【讨论】:

      【解决方案2】:

      像这样构建自定义单元格

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多