我正在使用swift 3.0中的一个项目,我有UIViewController来填充几个文本字段。因此,我使用表格视图作为下拉菜单,一旦选择了一行,它将被分配给UILabel。此外,我添加了一个UITapGuestureRecognizer方法,以确保在点击屏幕后隐藏键盘。不幸的是,由于这种方法,我的didSelectRow无法正常工作,实际上一旦点击一行,该名称将不会被分配给UILable。但是,当我消除UITapGusture方法时,它工作正常。我怎样才能解决这个问题并让这个工作?代码如下所示。
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() {
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
}