委托方法在tableviewcell中进行segue

问题描述 投票:0回答:2

我有一个委托方法,如果我按下tableview中的按钮,它应该转到另一个视图控制器并传递数据但它似乎不起作用。

func goToVC(uid: String) { //delegate method
    performSegue(withIdentifier: "showVC", sender: self) //Do I need this
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showVC", sender: self)
    self.tableView.deselectRow(at: indexPath, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showVC" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let guestVC = segue.destination as! GuestViewController
            guestVC.ref = userArray[indexPath.row].ref
        }
    }
swift uitableview uistoryboardsegue
2个回答
0
投票
class MainViewController: UIViewController {

    // set the cell's delegate in the data source
    // pass the object to the cell from the data source
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell.mainViewControllerDelegate = self
        cell.object = someArray[indexPath.row]

    }

    // this is the method that gets called by the cell through the delegate
    func pushToViewController(object: YourDataObject) {

        let destination = SomeViewController()
        destination.object = object
        navigationController?.pushViewController(destination, animated: true)

    }

}

class TheTableViewCell: UITableViewCell {

    // create a delegate and a data object
    var mainViewControllerDelegate: MainViewController?
    var object: YourDataObject?

    // this is the method that gets called when the button in the cell is tapped
    @objc func buttonAction() {

        mainViewControllerDelegate?.pushToViewController(object: object)

    }

}

我强烈建议初学者不要使用Interface Builder。你越早使用它,你就会越快理解。 Interface Builder是初学者的傻瓜。


0
投票

你这里不需要委托方法。如果需要从子视图控制器传递值,则可以使用Delegate方法。

你在做什么是完全正确的。确保正确设置故事板中的segue标识符。

还有一件事不要将你的表IBOutlet设置为默认值tableView尝试为toDoTable设置一个名为apt的表,因此它很容易调试。

© www.soinside.com 2019 - 2024. All rights reserved.