ios - 获取从 UITableViewController 中选择的行

标签 ios swift uitableview swift4

我正在尝试在我的 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/

相关文章:

ios - UIScrollView、UITableView 和约束问题

iOS11 核心 NFC 和 ISO 14443

ios - 没有收到来自 LinkedIn Objective-C 的电子邮件

ios - 为什么在构建 iOS 应用程序 (Swift) 时会收到此警告?

iphone - UITableView insertRowsAtIndexPaths :

swift - 无法向 UITableView 中的部分添加行

ios - 在 uiscrollview 中向 uiimageview 添加手势(平移、双击、捏合)

ios - 从设备运行而不是从构建和运行运行时应用程序崩溃

swift - UiSearchController 到 Swift 中的 UiCollectionView

block - 如何在 swift 中使用 NSURLConnection completionHandler