ios - 自定义单元格中未调用 UITableView didSelectRowAt indexPath

标签 ios iphone swift uitableview didselectrowatindexpath

我创建了新的 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,另一个用于测试。

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

enter image description here -这是第二个viewcontroller不能选择行

这是我的主视图 Controller

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

        }

    }

}

最佳答案

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

关于ios - 自定义单元格中未调用 UITableView didSelectRowAt indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258226/

相关文章:

iphone - 如何在ios中加粗按钮文本?

ios - instantiateViewControllerWithIdentifier 在哪里使用

xcode - UITextViewDelegate 函数应该在 Xcode 8.0 中与 URL 交互

ios - 在 Xcode 中调整图像大小?

html - iOS HTML5 音频控件未显示

ios - SpriteKit 中的持续运动

带有开发人员配置文件的设备上的 iphone ipa 文件 - 它将部署在不同 MAC 机器上的设备上

ios - 如何提取推送通知的部分内容?

ios - iphone 中的锁定方向问题

swift - UIActivityViewControllerCompletionWithItemsHandler 新更新 swift 出错