从searchBar绑定到RxSwift中的TableView

时间:2018-04-13 02:53:58

标签: ios swift rx-swift

UISearchBar and SearchDisplayController嵌入了Main.storyboard s' UIViewController。下面我有UIView。我正在尝试使用UITableView将用户输入从RxSwift直接导入UISearchBar,并执行结果&#39}。中间过滤。几乎它是我试图实现的自动完成功能。不知怎的,我收到了一个错误:

  

线程1:致命错误:从Optional()转换为UISearchBarDelegate失败

以下是我的代码:

UITableView

我在这里做错了什么?任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:2)

我无法直接输入输入,但是分两步完成:

     searchBar.rx.text
        .orEmpty
        .subscribe(onNext: {query in
            self.shownGenes.value = AllGenes.filter { $0.hasPrefix(query) }
        })

        self.shownGenes
            .asObservable()
            .bind(to: tableView.rx.items(cellIdentifier: cellIdentifier,
                                     cellType: GeneSeachTableViewCell.self)) {  row, gene, cell in
                                        cell.textLabel?.text = "\(gene) \(row)"
        }
        .disposed(by: disposeBag) 

shownGenes = Variable<[String]>([])。我还删除了UISearchBar and SearchDisplayController并将其替换为UISearchBar

答案 1 :(得分:0)

在故事板中将UISearchBar委托设置为我的视图控制器时,我遇到了这个问题。尝试在界面构建器中打开情节提要,然后右键单击搜索栏。如果看到委托的连接,则将其删除。希望有帮助

相关问题