ios - Swift indexPathForSelectedRow 从 subview 返回 nil

标签 ios swift uitableview tableview swift4

我有一个带有表的 subview ,并且我正在很好地填充数据。我有来自 super View 的“prepare(for segue:”),可以很好地识别单元格的选择。但是,从 super View 的准备函数中,indexPathForSelectedRow返回nil。即使我选择了单元格。

self.crossRefTVC.tableView.indexPathForSelectedRow //returns nil

其他一切工作正常,包括从主 super View 填充 crossRefTVC.tableview 数据。

添加此详细信息:

class MyText: UIViewController, UIGestureRecognizerDelegate, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet var crossRefTable: UITableView!
    var crossRefTVC = CrossRefTVC()

    override func viewDidLoad() {
        super.viewDidLoad()

        crossRefTable.delegate = crossRefTVC
        crossRefTable.dataSource = crossRefTVC
    }

    @objc func myMethodToHandleTap(_ sender: UITapGestureRecognizer) {
            crossRefTVC.results.append(data)
            crossRefView.center = view.center
            view.addSubview(crossRefView)
    }
...
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "CRShowDetail" {
            print(self.crossRefTVC.tableView)
            print(self.crossRefTVC.tableView.indexPathForSelectedRow as Any)

        }
    }

class CrossRefTVC : UITableViewController {

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      //other table overrided funcs here, all work.
}

就像我说的,准备代码正确执行,它可以访问 crossRefTVC 中的变量,但由于某种原因看不到所选单元格。

您可以在此处查看此 subview 的 Storyboard:

enter image description here

最佳答案

我删除了 crossRefTVC 并将 UITableViewDataSource、UITableViewDelegate 添加到 MyText 类,然后将其用于所有与表相关的代码。这解决了这个问题。虽然我真的不想把所有代码都放在这个主类中,但至少它现在可以工作了。谢谢

关于ios - Swift indexPathForSelectedRow 从 subview 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49482059/

相关文章:

ios - 为什么元素会无缘无故停止显示?

ios - 在没有源代码的情况下在客户端的企业应用程序商店上发布 IOS 应用程序

ios - swift 中的日期转换和日期差异问题

ios - AVPlayerItem 加载未知文件扩展名(缓存文件)

swift - 不能使数字为负

iphone - UIView 中的 UITableViewController

ios - UITableview 委托(delegate)方法未执行

ios - 蓝牙扫描启动 SwiftUI 后渲染列表

swift - 从 CLLocationManager (Google Maps API) 获取用户位置的问题

iOS/Swift/Firebase 身份验证 : Help Needed on Accessing "isNewUser"