【问题标题】:When a row selected, tableView didSelectRowAt not called in SearchTextFieldWhen a row selected, tableView didSelectRowAt not called in SearchTextField
【发布时间】:2019-12-17 21:16:22
【问题描述】:

在我的 IOS swift 项目中,我使用 SearchTextField ,用于可搜索的下拉组合框。 几天前我的代码很好。但突然我意识到我的 searchTextField 不能正常工作。

当我开始输入超过 3 个字符时,它开始显示结果来自 web 服务,正如预期的那样。 但是当我从 tableView 中选择一个单元格时,什么也没有发生。 我发现当我从 list(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 数据源和委托都可以。
我的视图控制器中没有手势识别器。
我打开了一个空的新项目,只添加了 SearchTextField pod 和一个 uitextfield,它工作正常。 我认为我的项目或故事板效果中的某些内容确实选择了方法。
是什么导致了这个问题?
有什么想法吗?

【问题讨论】:

  • 您需要确保您的搜索表视图作为视图控制器的实例变量保留在内存中。
  • 假设您正在输入的搜索字段位于另一个表视图的单元格中,例如库的 README.md 中的示例,并假设您描述的问题是与显示您键入时可能匹配的列表,您确定 那个 的代表设置正确吗?
  • @Jim mysearchfield 是一个使用 SearchTextField.swift 的 UITextField。我创建了新的 viewControIler 并没有工作,但是我创建了一个空的新项目并且它工作了。

标签: swift uitableview combobox dropdown didselectrowatindexpath


【解决方案1】:

我解决了我的问题。
这是解决方案:
我也在使用 IQKeyboard 吊舱。
在我的 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2022-12-21
    • 2021-06-24
    • 2011-04-18
    相关资源
    最近更新 更多