【问题标题】:UITableView didSelectRowAt indexPath not called in custom cellUITableView didSelectRowAt indexPath 未在自定义单元格中调用
【发布时间】:2016-10-26 09:14:15
【问题描述】:

我创建新的 ViewController 并拖动新的 UITableView 它可以工作,但是在我之前的两个 ViewController 中不能工作。

我已经完成了以下所有操作:

  1. UITableViewDelegate .delegate=self
  2. UITableViewDataSource .datasource=self
  3. Swift3 是 didSelectRowAt 不是 didDeselectRowAt
  4. .setEditing(true, animated: true)
  5. .allowsSelection = true
  6. .allowsSelectionDuringEditing = true
  7. 单选

我所有的数据都可以加载到tableview中,唯一的问题是不能选择行,在MainViewController中实际上我只有一个table view,另一个用于测试。

- 这是我的tableview无法选择行但可以加载数据的viewcontroller之一,这里我只有一个tableview,但是为了测试我创建了一个新的table view

- 这是第二个视图控制器无法选择行

这是我的 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)
        }
    }
}

这是我的 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

        }

    }

}

【问题讨论】:

  • 如何呈现第二个表格视图?包含它的视图是否允许用户交互?
  • func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // 如果检查表的条件并给出它的返回值 }
  • 在哪个控制器中,Didselect 方法不起作用?
  • 我删除了一张表并留下了一张但仍然不起作用,我添加 self.view.isUserInteractionEnabled = true 也不能。我可以在 tableview 中选择行,当我点击时该行将显示为灰色,但不调用 didSelectRowAt indexPath
  • 两个控制器都不工作

标签: ios iphone swift uitableview didselectrowatindexpath


【解决方案1】:

您很可能在某处添加了一个点击手势来取消单元格点击。在这里查看答案:https://stackoverflow.com/a/9248827/1286291

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多