1

This is a weird (but should be simple) one. I am simply trying to tap a custom UITableViewCell and trigger a segue to go to another ViewController. didSelectRowAt indexPath does not trigger when the cell is tapped, but it oddly enough does when the cell is swiped from right to left or left to right.

My didSelectRowAt indexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
   print("You selected cell number: \(indexPath.row)!") 
   self.performSegue(withIdentifier: "mySegue", sender: self)
}

(as suggested here)

My ViewController:

import UIKit

class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate {

    var myData = NSDictionary()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchMyData()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = 100
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func fetchMyData() {
        // ... fetches my data
    }


    ////////////////////////////////////////////////
    //Table view data source


    //set number of sections in table
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    //set number of rows in table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myData.count
    }

    //delegate information to cells in table rows
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("running tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell...")
        // Dequeue cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myCustomCell

        cell.cellAmount!.text = "Hello World"
        cell.cellTextField2!.text = "Some info"
        cell.cellTextField3!.text = "Some other info"
        cell.backgroundCardView.backgroundColor = UIColor.red
        cell.backgroundCardView.layer.cornerRadius = 8.0
        cell.backgroundCardView.layer.masksToBounds = false
        cell.backgroundCardView.layer.shadowColor = UIColor.black.withAlphaComponent(0.9).cgColor
        cell.backgroundCardView.layer.shadowOffset = CGSize(width: 0, height: 0)
        cell.backgroundCardView.layer.shadowOpacity = 0.9

        print("finished tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell...")
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected cell number: \(indexPath.row)!")
        self.performSegue(withIdentifier: "mySegue", sender: self)
    }

    //    func tableView(_ tableView: UITableView,  editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    //        let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
    //            // delete item at indexPath
    //        }
    //        let share = UITableViewRowAction(style: .normal, title: "Disable") { (action, indexPath) in
    //            // share item at indexPath
    //        }
    //
    //        share.backgroundColor = UIColor.blue
    //
    //        return [delete, share]
    //    }

    func tableView(_ tableView: UITableView,  editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        return []
    }

    // Reload the table data when a change is made
    //    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    //        self.tableView.reloadData()
    //    }

    /*
     // Override to support conditional editing of the table view.
     override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */

    /*
     // Override to support rearranging the table view.
     override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

     }
     */

    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */


    //Table view data source (end)
    ////////////////////////////////////////////////
}

I have already tried the following as well:

Does anyone know what could possibly be causing didSelectRowAt indexPath to not be triggered when the cell is tapped?

Rbar
  • 3,740
  • 9
  • 39
  • 69
  • FWIW: I had a similar [problem](https://stackoverflow.com/questions/44447365/uitableview-didselectrowat-is-not-called-ios-10-but-works-for-9-3-5). Ultimately, I wrongly designed the xib, but was only made aware when I migrated to iOS 10 from iOS 9. – AgRizzo Jul 16 '17 at 03:06
  • Thanks for sharing @AgRizzo. I double-checked and the UITableViewCell is within a UITableView. I also updated the question as I just noticed that swiping in either direction triggers the `didSelectRowAt IndexPath` but tapping still does not – Rbar Jul 16 '17 at 03:14
  • have you initiated the segue from view controller or from the tableview. By this I mean on the storyboard. Have you dragged and dropped from the tableview ? – Dark Innocence Jul 16 '17 at 11:41
  • @DarkInnocence I have tried both dragging the segue from the cell (on the storyboard) to the other view controller, and have tried dragging from the one view controller (that contains the tableview) to the destination view controller. I also added the `print` statement to the `didSelectRowAt IndexPath` to see if t was being called but the segue not triggered when the cell was tapped. It is not triggered at all when the cell is tapped. Though it is triggered and the segue is executed when the cell is swiped – Rbar Jul 16 '17 at 14:58
  • Try to use live debugging to see slices of your UI elements. There might be an overlay which blocks the interactions to your tableview. – erenkabakci Jul 16 '17 at 16:24
  • @erenkabakci, can you clarify what you mean by `use live debugging to see slices`? If I'm understanding correctly, you saying for me to look at my console? No logs there to give hints that I see, and no errors are being thrown so I'm not able to look at, for example, the stack trace that led to an error – Rbar Jul 16 '17 at 17:14
  • On your custom cell, do you have any UITapGestures? or any other interaction with elements or cell itself? – Miknash Jul 16 '17 at 18:26
  • @miknash I do not have any other interaction with the cell or its elements (including UITapGestures). Just a custom cell with some textFields (for which `User Interaction Enables = False`). – Rbar Jul 16 '17 at 18:28
  • @Rbar you can find a detailed tutorial here. https://www.raywenderlich.com/98356/view-debugging-in-xcode-6 – erenkabakci Jul 17 '17 at 07:28
  • 1
    Thanks for sharing, @erenkabakci! I took a look at the View Debugging section in XCode for my project (pretty cool stuff!) and it looks like there are no views on top of my custom UITableViewCells that may be recieving the touch instead of my cell. Oddly enough, I rebuilt the whole ViewController, UITableView, and UITableViewCell (admittedly out of frustration) and the new one works with no problems.. same code, same design, same settings.. The curiosity in me would really love to know what caused the issue the first time, but I am glad it is at least logically working now! – Rbar Jul 17 '17 at 14:04

1 Answers1

0

I faced the same issue and found an answer that was applicable for me here. I had this code for keyboard dismiss in viewWillAppear:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

After commenting this line tapping started to work! But now I will have to find better way for keyboard dismiss. :) Kindly check your exstensions, probably you used similar solution for keyboard dismiss.

DJ-Glock
  • 1,277
  • 2
  • 12
  • 39
  • Ah, apologies. I missed that question is very old, but who knows, may be it will help anybody else. – DJ-Glock Aug 19 '17 at 23:03