gpt4 book ai didi

swift - 在tableviewcell中进行segue的委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-30 11:54:11 28 4
gpt4 key购买 nike

我有一个委托(delegate)方法,如果我按下表格 View 中的按钮,它应该转向另一个 View Controller 并传递数据,但它似乎不起作用。

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
}
}

最佳答案

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 对于初学者来说是愚人节。

关于swift - 在tableviewcell中进行segue的委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48011791/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com