gpt4 book ai didi

ios - 获取从 UITableViewController 中选择的行

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

我正在尝试在我的 segue 中使用所选行的索引。我有一个 UITableViewController 作为类类型,所以我知道它有委托(delegate)和数据源,但是当我尝试使用 DidSelectRowAt indexPath: indexPath 函数时,该函数永远不会被调用。任何帮助都会很棒。我对此做了一些研究,其他人有一个 UIView,上面有一个 TableView,但没有单独的 TableView。

import UIKit
import GoogleBooksApiClient

class SearchResultsTableViewController: UITableViewController {
var titleFromSearch: String?
var authorFromSearch: String?

var index = 0
var data = [Volume]()

override func viewDidLoad() {
super.viewDidLoad()

.............................

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
................
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
index = indexPath.row
performSegue(withIdentifier: "details", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailsViewController{
destination.titleOfBook = data[index].volumeInfo.title
}
}

}

我仍然保留着同样的问题。我真的很感谢大家的所有评论和帮助,但我仍然无法让它发挥作用。我尝试使用局部变量来与使用 PerformSegue 传递 IndexPath 一起使用,但我仍然遇到调用 DidSelectRowAt 以及实际上能够使用所选行的问题。

最佳答案

tableView(_:didSelectRowAt:)的方法名称必须完全匹配:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

如果可选委托(delegate)方法未正确命名,则不会调用它们。

当您通过调用performSegue(withIdentifier:sender:)从该方法调用segue时,您可以在sender参数中传递所选单元格的indexPath,然后在您的中使用该indexPath >prepare(for:sender:) 方法

关于ios - 获取从 UITableViewController 中选择的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49121925/

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