UITableViewCell 不能用一根手指点击,但可以用两根手指点击

TableViewCell is not clickable with one finger tap, but it is with two fingers

我创建了一个 table 视图,tableViewCell 不能用一根手指点击,但是当我尝试用两根手指点击 tableViewCell 时,点击事件发生了.我不知道为什么会这样。我在 tableView.

中创建了一个自定义单元格

邀请VC

import UIKit

class InvitePeopleVC: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    var nameArray = ["Alwin Lazar", "Ajith Ramesh CR", "Ebrahim KK", "Vishnu Prakash"]
    var emailArray = ["alwin@xeoscript.com", "ajith@xeoscript.com", "ebrahim@xeoscript.com", "vishnu@xeoscript.com"]

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var doneImg: UIImageView!
    @IBOutlet weak var nameTextFld: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        delegates()
        uiModifications()
        gestureRecognizers()
    }

    func delegates() {
        tableView.dataSource = self
        tableView.delegate = self
        nameTextFld.delegate = self
    }

    func uiModifications() {
        nameTextFld.attributedPlaceholder = NSAttributedString(string: "Name or email address", attributes: [NSForegroundColorAttributeName: UIColor.white])
    }

    func gestureRecognizers() {
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.dismissKeyboard)))
        self.doneImg.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.doneImgPressed)))
    }

    func dismissKeyboard() {
        nameTextFld.resignFirstResponder()
    }

    func doneImgPressed() {
        print("done Image tapped")

    }

    func inviteBtnPressed() {
        print("invite button pressed")
    }

    // UITextFieldDelegate method
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        if textField == self.nameTextFld {

            self.nameTextFld.resignFirstResponder()

        }
        return true

    }

    // TableView DataSource methods
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "InviteCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! InviteCell

        cell.nameLbl.text = nameArray[indexPath.row]
        cell.emailLbl.text = emailArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    // TableView Delegate methods
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("selected row is \(indexPath.row)")
    }



    @IBAction func backBtnPressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
}

#InviteCell

import UIKit

class InviteCell: UITableViewCell {

    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var emailLbl: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}

UIViewController 图片

TableView 属性检查器

InviteCell 属性检查器

在上面的代码中,我试图用一根手指 select 一个单元格,但是 selection 没有发生。

提前致谢...

您的设置代码中有以下行:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.dismissKeyboard)))

这会为您的整个视图设置一个手势识别器,并且会吞没主视图上的任何触摸。如果你删除它,你应该让 table 单元格选择正常工作:)

您在代码中添加的点击手势导致了问题。 Tapgesture 识别器正在侦听视图中的用户点击操作。单元 select 侦听器被添加的点击手势阻止。

正如@Fahim 所说,如果您从代码中删除点击手势,那么单元格 selection 将顺利运行。

处理点击问题的更优雅的方法是:

          let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AppController.dismissKeyboard))

          view.addGestureRecognizer(tap)

          //this is the KEY of the fix
          tap.cancelsTouchesInView = false

通过这种方式,您可以保留手势识别器,并且仍然可以在一个 tap/selection 中获得 table 查看操作。