Yang Xu
Yang Xu

Reputation: 41

UITableView didSelectRowAt indexPath not called in custom cell

I create new ViewController and drag new UITableView it works, but in my previous two ViewControllers cannot work.

All the following I already done:

  1. UITableViewDelegate .delegate=self
  2. UITableViewDataSource .datasource=self
  3. Swift3 and is didSelectRowAt not didDeselectRowAt
  4. .setEditing(true, animated: true)
  5. .allowsSelection = true
  6. .allowsSelectionDuringEditing = true
  7. SingelSelection

and all my data can be load into tableview, only thing is cannot select rows, in MainViewController actually I only have one table view, another one is used for testing.

enter image description here -This is one of the viewcontroller my tableview cannot select row but can load data, here I only have one tableview, but to test I create a new table view

enter image description here -This is the second viewcontroller cannot select row

This is my MainViewController

extension MainViewController: UITableViewDelegate, UITableViewDataSource {

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

        return 10
    }

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

        if tableView == self.tableview_restaurants {

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

            cell.label_name.text = "OMG Restaurant # \(String(indexPath.row))"

            return cell
        }
        else if tableView == self.tableview_test {

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

            cell.label_test.text = String(indexPath.row)

            return cell
        }
        else {
            let cell = UITableViewCell()

            return cell
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        //tableView.deselectRow(at: indexPath, animated: true)
        if tableView == self.tableview_restaurants {

        }
        else if tableView == self.tableview_test {
            print("select this row")
            print(indexPath)
        }
    }
}

This is my FilterViewController

extension SearchFilterViewController: UITableViewDelegate, UITableViewDataSource {


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

        var numberofrowsinsecion:Int = 0

        if (tableView == self.tableview_quickfilters) {

            numberofrowsinsecion = json_quickfilters["filters"].arrayValue.count
            //print(numberofrowsinsecion)
        }

        else if (tableView == self.tableview_cuisinetype) {
            numberofrowsinsecion = json_cuisinetype["cuisine_types"].arrayValue.count
            //print(numberofrowsinsecion)
        }

        return numberofrowsinsecion
    }


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

        if (tableView == self.tableview_quickfilters) {

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

            let filter = json_quickfilters["filters"].arrayValue

            if let filter_name = cell.label_quickfiltersname {

                filter_name.text = filter[indexPath.row]["name"].stringValue
                filter_name.font = Common.Config.FontSize.XL_L
            }
            return cell
        }

        else if (tableView == self.tableview_cuisinetype) {

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

            let cuisinetype = json_cuisinetype["cuisine_types"].arrayValue

            if let cuisinetype_name = cell.label_cuisinetypename {

                cuisinetype_name.text = cuisinetype[indexPath.row]["name"].stringValue
                cuisinetype_name.font = Common.Config.FontSize.XL_L
            }
            return cell
        }

        else {
            let cell = UITableViewCell()
            return cell
        }

    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if (tableView == self.tableview_quickfilters) {

            print(indexPath)

            //let cell = tableView.cellForRow(at: indexPath) as! QuickFiltersCell

        } else if (tableView == self.tableview_cuisinetype) {

            print(indexPath)

            //let cell = tableView.cellForRow(at: indexPath) as! CuisineTypeCell

        }

    }

}

Upvotes: 1

Views: 5952

Answers (1)

Nikolay Spassov
Nikolay Spassov

Reputation: 1316

You most likely have added a tap gesture somewhere that cancels the cell taps. Check the answer here: https://stackoverflow.com/a/9248827/1286291

Upvotes: 4

Related Questions