简体   繁体   English

didSelectRowAt Swift 3即使在设置委托和dataSource后也不调用

[英]didSelectRowAt Swift 3 Not Called even after delegate and dataSource are set

This question seems to have been asked many times on StackOverflow, but after searching for hours, I still haven't found the solution to my issue. 在StackOverflow上似乎已经多次询问过这个问题,但在搜索了几个小时之后,我仍然没有找到解决我问题的方法。

I have a table view in SecondViewController.swift and I did make it delegate and dataSource SecondViewController.swift , but the didSelectRowAt method is still not fired when a table cell is clicked. 我在SecondViewController.swift中有一个表视图,我确实使它成为委托dataSource SecondViewController.swift ,但是当单击表格单元格时,仍然没有触发didSelectRowAt方法。 I am trying to apply a segue to another view controller, but the didSelectRowAt method isn't working itself. 我试图将segue应用于另一个视图控制器,但didSelectRowAt方法本身不起作用。

I have selected Single Selection in my storyboard in the table view properties, and I have made sure that user interaction is enabled for the table view and the prototype cell. 我在表视图属性的故事板中选择了单选 ,我确保为表视图和原型单元启用了用户交互

For the SecondViewController.swift below, please assume that allEvents are populated into the allEvents array. 对于下面的SecondViewController.swift ,请假设allEvents填充到allEvents数组中。 I was able to successfully populate them and display them in the table. 我能够成功填充它们并在表格中显示它们。 The only problem is clicking on the row to perform a segue. 唯一的问题是单击该行以执行segue。 Also, please ignore the awkward indentation below. 另外,请忽略下面的尴尬缩进。

SecondViewController.swift SecondViewController.swift

import UIKit

class SecondViewController: UIViewController, UITableViewDataSource,    UITableViewDelegate {

  @IBOutlet var tableView: UITableView!

  var allEvents: Array<JSON> = []

  override func viewDidLoad() {

    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    //self.tabBarItem.image = myTabImage
    self.tabBarController?.tabBar.isTranslucent = false
    self.tabBarController?.tabBar.barTintColor = UIColor(red:0.25, green:0.25, blue:0.25, alpha:1.0)
    self.tabBarController?.tabBar.tintColor = UIColor.white

    //ASSUME ALL EVENTS ARE POPULATED INTO allEvents

  }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: Tableview Datasource
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return allEvents.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 150
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("SEGUE")
    self.performSegue(withIdentifier: "oneSegue", sender: allEvents[indexPath.row])
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "eventViewTableCell", for: indexPath) as! EventViewTableCell        

    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // let detailController = segue.destination as! EventDetailsController


}
}

I have been on this for hours and still have not found out the problem. 我已经在这几个小时了,仍然没有发现问题。 I would greatly appreciate any help! 我非常感谢任何帮助!

You may want to check your tableview again, do choose single selection. 您可能想再次检查您的tableview,选择单一选择。

I'm assuming you are selecting No Selection as the picture bellow 我假设你选择No Selection作为下面的图片

在此输入图像描述

Hope it helps. 希望能帮助到你。

I had the same problem on a Controller within a TabBarController. 我在TabBarController中的控制器上遇到了同样的问题。

What fixed it for me was this: 为我修好的是:

Disable the User Interaction for the Content view 禁用“ 内容”视图的“用户交互”

在此输入图像描述

My guess is that it was becoming the responder so it removed the event from the TableView 我的猜测是它成为响应者所以它从TableView中删除了事件

Note: Since the viewDidLoad assignments to delegate and dataSource became redundant after setting it up in the StoryBoard, I removed them, although leaving them does no harm. 注意:由于在StoryBoard中设置后, dataSourcedelegatedataSource赋值变得多余,所以我删除了它们,尽管它们没有任何损害。

If you do nothing else than calling the segue in didSelectRowAt remove the entire method and connect the segue in Interface Builder to the table view cell (rather than to the controller). 如果除了在didSelectRowAt中调用segue之外什么都不做,则删除整个方法并将Interface Builder中的segue连接到表视图单元(而不是控制器)。 The sender parameter will contain the reference to the selected table view cell. sender参数将包含对所选表视图单元格的引用。

I suspect when you collected and populated your allEvents array did you collect any touch events in there ? 我怀疑当你收集并填充你的allEvents数组时,你收集了那里的任何触摸事件吗? or have you used UIGestureRecognizer in this or it's super view ? 或者你在这个或它的超级视图中使用过UIGestureRecognizer? If you have intercepted any touch event to populate your array or any other purpose comment it out and test it with a simple preloaded array like : 如果您截获了任何触摸事件以填充您的数组或任何其他目的,请将其注释掉并使用简单的预加载数组进行测试,例如:

let dataSource = ["1", "2", "3", "4"]

let me know if this is not your case so I can find another solution. 让我知道如果这不是你的情况,所以我可以找到另一种解决方案。

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

相关问题 didSelectRowAt没有被调用,但是设置了委托和数据源 - didSelectRowAt not getting called but delegate and dataSource are set UITableView 要么没有被调用,即使我已经设置了它的委托、数据源并注册了可重用的单元格 - UITableView either not being called even though I have set its delegate, dataSource, and registered the reusable cell 即使设置了委托,MKMapview 委托也从未被调用 - MKMapview delegate never called even with set delegate 委托didSelectRowAt不为某些单元格调用 - Delegate didSelectRowAt Not being called for some cells 即使在[[UIPickerView alloc] init]之后设置了委托,也不会调用UIPickerView委托方法 - UIPickerView delegate methods not being called even though delegate has been set after [[UIPickerView alloc] init] 为什么即使在设置委托协议后也没有调用UIPickerView委托方法? - why UIPickerView delegate methods are not called even after setting delegate protocols? 按下行时不调用表视图委托方法 (didSelectRowAt) - Table View Delegate method (didSelectRowAt) not called when row pressed UITableview委托和数据源调用两次 - UITableview delegate and datasource called twice 委托即使设置为self也返回nil - delegate returns nil even after set to self 未调用 UITableViewController Delegate 和 DataSource 方法 - UITableViewController Delegate and DataSource method not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM