swift - single - Why can't I select TableView cells?




uitableview single selection swift (4)

This is the flow of my app:

First, the TableView is set to hidden. There is a UITextField in the center of the screen. When the user types something and hits Go, this code is run:

self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {

    self.textFieldConstraint.constant = -230
    self.tableView.hidden = false
    self.goButton.hidden = true
    self.view.layoutIfNeeded()

}, completion: nil)

At this point, the tableview is populated. When a row is selected, I need to manipulate the data that is populating it.

However, absolutely nothing happens when I tap a cell.

What am I doing wrong?

My TableView code is here:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: SearchResultsTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SearchResultsTableViewCell

    cell.label.text = searchResultsNames[indexPath.row]

    return cell
}

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

    return searchResultsUrls.count

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print("HELLO")

}

And, I have set the dataSource and delegate properly.

I also want to clarify that the tableView populates and scrolls properly; it just won't do anything when I tap a cell.

Update:

I've discovered that for some reason, I can select the cells when I press and hold them. It is not what I want, so does anybody know how to fix this?


For me I was implementing another did select row method, so I erased it and typed "didSelect..." and selected the first one in the suggested methods, which is this for swift 3:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Row #: \(indexPath)")
}

I have just used your code to create a simple table, selection is working fine and logging out HELLO as expected. Can you check the values of Selection in the attributes inspector? Here is mine, which has Selection set to Single Selection.

And here is the code I used for my simple table

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var searchResults = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        searchResults.append("Testing 1")
        searchResults.append("Testing 2")
        searchResults.append("Testing 3")
        searchResults.append("Testing 4")
        searchResults.append("Testing 5")

        tableView.dataSource = self
        tableView.delegate = self
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)
        cell.textLabel?.text = searchResults[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("HELLO")
    }
}

I also tried hiding and showing the tableView which made no difference on selection.

EDIT & SOLUTION:

In the comments below, we discovered that the issue is related to a tapGestureRecogniser on the view, This was identified by the op only being able to make a selection by holding a tap on the cell. The gesture has to fail before the selection can be made, the op managed to solve the problem by referring to this other SO Answer


In your viewDidLoad, or wherever you set up your view, make certain that your table view even allows selections. That can be controlled with the allowSelection property.

Something like:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.allowsSelection = true
}

My problem has been caused by the tap gesture recognizer on the view controller itself (I had BaseTableViewController I was extending from). Probably it was interfering with the gesture recognizers of UITableView.





uitableview