本文介绍了不能用一只手指点击TableViewCell,但是用两只手指可以单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. I don't know why this occurres. I created a custom cell in tableView.

import UIKit

class InvitePeopleVC: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    var nameArray = ["Alwin Lazar", "Ajith Ramesh CR", "Ebrahim KK", "Vishnu Prakash"]
    var emailArray = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"]

    @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图片

TableView属性检查器

UIViewController Images

TableView Attributes Inspector

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

In the code above, I'm trying to select a cell with one finger, but the selection does not happen.

预先感谢...

推荐答案

设置代码中包含以下行:

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 :)

这篇关于不能用一只手指点击TableViewCell,但是用两只手指可以单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 03:28