我有一个带有表的 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:
最佳答案
我删除了 crossRefTVC 并将 UITableViewDataSource、UITableViewDelegate 添加到 MyText 类,然后将其用于所有与表相关的代码。这解决了这个问题。虽然我真的不想把所有代码都放在这个主类中,但至少它现在可以工作了。谢谢
关于ios - Swift indexPathForSelectedRow 从 subview 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49482059/