gpt4 book ai didi

ios - DidSelectRow 方法由于 tapGestureRecognizer 而被禁用

转载 作者:行者123 更新时间:2023-11-30 12:56:22 25 4
gpt4 key购买 nike

我正在 swift 3.0 中开发一个项目,我有 UIViewController 来填充一些文本字段。因此,我使用 TableView 作为下拉菜单,一旦选择一行,它将被分配给 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() {
//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
}

最佳答案

你是对的,DidSelectRow方法会因为tapGestureRecognizer而被禁用。你可以尝试在'touchsbegin'中执行'view.endEditing(true)',记得重写super func。UITableview存在时不要使用tapGestureRecognizer。

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

关于ios - DidSelectRow 方法由于 tapGestureRecognizer 而被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40356951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com