didSelectRowAtIndexPath: not being called

前端 未结 14 2233
轻奢々
轻奢々 2020-11-29 18:57

I have a UITableView as a subview of my UIScrollVIew, which is the main view controlled by my MainViewController.

In MainView

14条回答
  •  迷失自我
    2020-11-29 19:07

    My problem is the cell is a customized cell, and the action does not work on it. In addition, there is a UITapGestureRecognizer defined in the superclass.

    Firstly, Set tapGesture.cancelsTouchesInView = false

        override func viewDidLoad() {
            super.viewDidLoad()
            
            initUI()
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(endEditing))
            tapGesture.cancelsTouchesInView = false
            view.addGestureRecognizer(tapGesture)
        }
    

    Secondly, Instead of setting isUserInteractionEnabled = true; in the table view, I set the action on the cell.

    In the ViewDidLoad()

            super.viewDidLoad()
            
            tableView.delegate = self
        }
    

    Then in the

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell: UITableView = tableView.dequeueReusableCell(for: indexPath)
            cell.isUserInteractionEnabled = true;
    

    You can try this solution if you are creating a customized cell.

提交回复
热议问题