view raw
danutha danutha - 6 months ago 31
iOS Question

DidSelectRow method being disabled due to tapGestureRecognizer

Im working on a project in swift 3.0 and I have UIViewController as to fill up few text fields. Thus, I have used a table view as a drop down menu, where once a row is been selected it'll get assigned to a UILabel. In addition I have added a UITapGuestureRecognizer method to make sure the keypad hides once the screen is been tapped. Unfortunately because of this method my didSelectRow is not working,in fact once a row is been tapped the name won't get assigned to the UILable. But when I eliminate the UITapGusture method it works fine. How can I solve this issue ? The code as bellow.

import UIKit
import CoreData

class AddRecurringExpensesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var selectCategoryLabel: UILabel!

@IBOutlet weak var expenseNameTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!
@IBOutlet weak var toTextField: UITextField!
@IBOutlet weak var fromTextField: UITextField!

@IBOutlet weak var recurringexpenseTypeTableView: UITableView!

@IBOutlet weak var fromDatePicker: UIDatePicker!
@IBOutlet weak var fromDatePickerView: UIView!

@IBOutlet weak var toDatePickerView: UIView!
@IBOutlet weak var toDatePicker: UIDatePicker!

var selectCategoryArray = ["Entertainment", "Food", "Membership", "Misc", "Purchase", "Subscription", "Transport", "Utility", "Other"]

override func viewDidLoad() {
self.fromDatePickerView.isHidden = true
self.toDatePickerView.isHidden = true
self.recurringexpenseTypeTableView.isHidden = true

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

func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectCategoryArray.count

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell ()

cell.textLabel?.text = selectCategoryArray[indexPath.row]
return cell

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

self.selectCategoryLabel.text = selectCategoryArray[indexPath.row]

print("selected row is \(selectCategoryLabel.text)")

self.recurringexpenseTypeTableView.isHidden = true
@IBAction func selectCategoryButtonPressed(_ sender: AnyObject) {
self.recurringexpenseTypeTableView.isHidden = false

@IBAction func fromDateButtonPressed(_ sender: AnyObject) {

self.fromDatePickerView.isHidden = false

@IBAction func fromDateAddedButtonPressed(_ sender: AnyObject) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
fromTextField.text = dateFormatter.string(
self.fromDatePickerView.isHidden = true

@IBAction func toDateButtonPressed(_ sender: AnyObject) {
self.toDatePickerView.isHidden = false

@IBAction func toDateAddedButtonPressed(_ sender: AnyObject) {
let dateFormatter = DateFormatter ()
dateFormatter.dateFormat = "dd MMM yyyy"
toTextField.text = dateFormatter.string(from:
self.toDatePickerView.isHidden = true


You are right,DidSelectRow method will be disabled due to tapGestureRecognizer。 you can try execute 'view.endEditing(true)'in 'touchsbegin',remember to rewrite super func.Don't use tapGestureRecognizer when UITableview exists.

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)