简体   繁体   English

Swift indexPathForSelectedRow从子视图返回nil

[英]Swift indexPathForSelectedRow returns nil from subview

I have a subview with a table and I'm populating the data just fine. 我有一个带表的子视图,并且可以很好地填充数据。 I have the "prepare(for segue:" from the superview recognizing the selection of the cell just fine. However, from the prepare function in the super the indexPathForSelectedRow returns nil. Even though I have the cell selected. 我从超级视图中获得了“ prepare(for segue:”),可以很好地识别出单元格的选择。但是,从超级列表中的prepare函数中,indexPathForSelectedRow返回nil。

self.crossRefTVC.tableView.indexPathForSelectedRow //returns nil

Everything else works fine, including populating crossRefTVC.tableview data from the main superview. 其他一切工作正常,包括从主超级视图填充crossRefTVC.tableview数据。

Adding this detail: 添加此详细信息:

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

like I said, the prepare code executes correctly, and it can access the variables in crossRefTVC, but it can't see the selected cell for some reason. 就像我说的那样,准备代码可以正确执行,并且可以访问crossRefTVC中的变量,但是由于某种原因它看不到所选单元格。

You can see the storyboard for this subview here: 您可以在此处查看此子视图的情节提要:

在此处输入图片说明

I removed crossRefTVC and added UITableViewDataSource, UITableViewDelegate to MyText class, and just used that for all the table related code. 我删除了crossRefTVC并将UITableViewDataSource和UITableViewDelegate添加到MyText类,并将其用于所有与表相关的代码。 This solved the issue. 这解决了问题。 Though I didn't really want to have all the code in this main class, but at least it works now. 尽管我并不是很想在这个主类中拥有所有代码,但至少现在可以了。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM