不能用一只手指点击TableViewCell,但是用两只手指可以单击

Alw*_*win 2 uitableview uigesturerecognizer ios swift

我创建了一个表格视图,并且用一个手指无法单击tableViewCell,但是当我尝试用两个手指单击tableViewCell时,将发生单击事件。我不知道为什么会这样。我在tableView中创建了一个自定义单元格。

邀请函

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

#InviteCell

import UIKit

class InviteCell: UITableViewCell {

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

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}
Run Code Online (Sandbox Code Playgroud)

UIViewController图片

TableView属性检查器

在此处输入图片说明

InviteCell属性检查器

在此处输入图片说明

在此处输入图片说明

在上面的代码中,我试图用一根手指选择一个单元格,但是选择不会发生。

提前致谢...

Fah*_*him 6

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

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(InvitePeopleVC.dismissKeyboard)))
Run Code Online (Sandbox Code Playgroud)

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


Fid*_*del 5

处理水龙头问题的一种更优雅的方法是:

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

          view.addGestureRecognizer(tap)

          //this is the KEY of the fix
          tap.cancelsTouchesInView = false
Run Code Online (Sandbox Code Playgroud)

这样一来,您可以保持手势识别器,并且只需单击一下即可获得表格视图操作。