在我的 IOS swift 项目中,我使用 SearchTextField ,用于可搜索的下拉组合框。 几天前,我的代码还不错。但突然我意识到我的 searchTextField 没有按应有的方式工作。
当我开始输入超过 3 个字符时,它开始显示来自 Web 服务的结果,正如预期的那样。 但是当我从 tableView 中选择一个单元格时,什么也没有发生。 我发现当我从列表(tableView)中选择一个项目时,代码不会在 SearchTextField.swift 中输入以下方法
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if itemSelectionHandler == nil {
self.text = filteredResults[(indexPath as NSIndexPath).row].title
} else {
let index = indexPath.row
itemSelectionHandler!(filteredResults, index)
}
clearResults()
}
并且也不在我的 viewcontroller.swift 中输入“itemSelectionHandler”,而是输入“userStoppedTypingHandler”
@IBOutlet weak var mySearchTextField: SearchTextField!
override func viewDidLoad() {
super.viewDidLoad()
mySearchTextField.userStoppedTypingHandler = {
if let criteria = self.mySearchTextField.text {
if criteria.count > 3 {
......
print("enters here")
}
}
}
mySearchTextField.itemSelectionHandler = { filteredResults, itemPosition in
print("not enters here, cell selected")
.....
.....
}
}
但是代码进入这个方法并在SearchTextField.swift中显示结果,
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cell ok")
var cell = tableView.dequeueReusableCell(withIdentifier: SearchTextField.cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: SearchTextField.cellIdentifier)
}
......
......
return cell
}
TableView数据源和委托(delegate)都可以。
我的 View Controller 中没有gestureRecognizer。
我打开了一个空的新项目,只添加了 SearchTextField pod 和 uitextfield,它工作正常。
我认为我的项目或 Storyboard效果中的某些内容 didselectat 方法。
是什么原因导致这个问题?
有什么想法吗?
最佳答案
我解决了我的问题。
解决办法如下:
我也在使用 IQKeyboard pod。
在我的 AppDelegate.swift 文件中有 3 行 IQKeyboard 代码。
当我注释掉 shouldResignOnTouchOutside 行时,我的 didselectRowAt 函数被调用。
因为IQKeyboard的shouldResignOnTouchOutside使用了UITapGestureRecognizer
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
registerForPushNotifications()
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.enableAutoToolbar = false
//IQKeyboardManager.shared.shouldResignOnTouchOutside = true
return true
}
关于swift - 当选择一行时,tableView didSelectRowAt 未在 SearchTextField 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382193/