Sriharsha Guduguntla
Sriharsha Guduguntla

Reputation: 171

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.

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. I am trying to apply a segue to another view controller, but the didSelectRowAt method isn't working itself.

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. 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. Also, please ignore the awkward indentation below.

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!

Upvotes: 3

Views: 3571

Answers (4)

Efren
Efren

Reputation: 4887

I had the same problem on a Controller within a TabBarController.

What fixed it for me was this:

Disable the User Interaction for the Content view

enter image description here

My guess is that it was becoming the responder so it removed the event from the 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.

Upvotes: 1

Rhm Akbari
Rhm Akbari

Reputation: 360

I suspect when you collected and populated your allEvents array did you collect any touch events in there ? or have you used UIGestureRecognizer in this or it's super view ? 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.

Upvotes: 0

Xuan-Gieng Nguyen
Xuan-Gieng Nguyen

Reputation: 846

You may want to check your tableview again, do choose single selection.

I'm assuming you are selecting No Selection as the picture bellow

enter image description here

Hope it helps.

Upvotes: 12

vadian
vadian

Reputation: 285069

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). The sender parameter will contain the reference to the selected table view cell.

Upvotes: 0

Related Questions