简体   繁体   中英

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.

self.crossRefTVC.tableView.indexPathForSelectedRow //returns nil

Everything else works fine, including populating crossRefTVC.tableview data from the main superview.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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