簡體   English   中英

從searchBar綁定到RxSwift中的TableView

[英]Bind from searchBar to TableView in RxSwift

UISearchBar and SearchDisplayControllerMain.storyboard嵌入UIViewController S' UIView 下面我有UITableView 我正在嘗試使用RxSwift將來自UISearchBar的用戶輸入直接RxSwiftUITableView ,從而在中間進行結果過濾。 我正在嘗試實現的幾乎是自動完成功能。 不知何故我遇到了一個錯誤:

線程1:致命錯誤:無法從Optional()轉換為UISearchBarDelegate

這是我的代碼:

private func setUpAutocomplete() {
    // Define intput stream
    searchBar.rx.text
        .orEmpty
        .map({
            (query: String) -> [String] in
            let res = AllGenes.filter { $0.hasPrefix(query) }
            return res
        })
        .bind(to: tableView.rx.items(cellIdentifier: cellIdentifier,
                                     cellType: GeneSeachTableViewCell.self)) {  row, gene, cell in
                                        cell.textLabel?.text = "\(gene) \(row)"
        }
        .disposed(by: disposeBag)

} 

我在這里做錯了什么? 任何建議將不勝感激。

我無法直接通過管道傳遞輸入,但是分兩個步驟完成了它:

     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

UISearchBar委托設置為情節提要中的視圖控制器時,出現了這個問題。 嘗試在界面構建器中打開情節提要,然后右鍵單擊搜索欄。 如果看到委托的連接,則將其刪除。 希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM