简体   繁体   English

只用一根手指点击就无法点击TableViewCell,但它有两根手指

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

I created a table view and the tableViewCell is not clickable with one finger, but when I try to click the tableViewCell with two fingers the click event takes place. 我创建了一个表视图,并且tableViewCell不能用一根手指点击,但当我尝试用两根手指点击tableViewCell时,会发生click事件。 I don't know why this occurres. 我不知道为什么会这样。 I created a custom cell in tableView. 我在tableView中创建了一个自定义单元格。

InviteVC InviteVC

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 #InviteCell

import UIKit

class InviteCell: UITableViewCell {

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

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}

UIViewController Images UIViewController图像

TableView Attributes Inspector TableView属性检查器

在此输入图像描述

InviteCell Atribute Inspector InviteCell Atribute Inspector

在此输入图像描述

在此输入图像描述

In the code above, I'm trying to select a cell with one finger, but the selection does not happen. 在上面的代码中,我试图用一根手指选择一个单元格,但选择不会发生。

Thanks in advance... 提前致谢...

A more elegant way of dealing with the tap issue is: 处理点击问题的更优雅的方法是:

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

          view.addGestureRecognizer(tap)

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

This way you can keep your gesture recognizer and still get the table view action in one single tap/selection. 通过这种方式,您可以保留手势识别器,并且只需一次点击/选择即可获得表格视图操作。

You have the following line in your set up code: 您的设置代码中包含以下行:

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

That sets up a gesture recognizer for your whole view and that would swallow any touches on the main view. 这为您的整个视图设置了一个手势识别器,这将吞噬主视图上的任何触摸。 If you remove that, you should get the table cell selection working correctly :) 如果你删除它,你应该让表格单元格选择正常工作:)

The Tap gesture you have added in the code is causing the issue. 您在代码中添加的Tap手势导致了问题。 Tapgesture recogniser is listening to the user tap actions in the view. Tapgesture识别器正在监听视图中的用户点击操作。 The cell select listner is being blocked by the added Tap gesture. 添加的Tap手势正在阻止单元格选择列表器。

As @Fahim said, if you remove the tap gesture from your code, then cell selection will work smoothly. 正如@Fahim所说,如果您从代码中删除了点击手势,那么单元格选择将顺利进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM