DidSelectRow method being disabled due to tapGestu

2019-03-05 22:13发布

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() {
    super.viewDidLoad()
    self.fromDatePickerView.isHidden = true
    self.toDatePickerView.isHidden = true
    self.recurringexpenseTypeTableView.isHidden = true

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

  }
 func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
  }
  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(from:fromDatePicker.date)
    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: toDatePicker.date)
    self.toDatePickerView.isHidden = true
  }

3条回答
2楼-- · 2019-03-05 22:52

You can see it as layers on top of each other. With the UITapGuestureRecognizer you have added a new layer. When the user presses on the screen, the top layer is called (UITapGuestureRecognizer). The layer under it will not be called.

For this situation, I suggest that you remove the UITapGuestureRecognizer layer. And add the dismiss of the Keyboard in the DidSelectRow.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-05 22:56

Modify your dismiss keyboard function as follows ,

func dismissKeyboard() {
  if self.recurringexpenseTypeTableView.isHidden == true {
     view.endEditing(true)
   }
}
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-03-05 22:57

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)
        view.endEditing(true)
    }
查看更多
登录 后发表回答