【问题标题】:How to bind UISearchBar to UITableView with RxSwift如何使用 RxSwift 将 UISearchBar 绑定到 UITableView
【发布时间】:2021-08-13 22:47:53
【问题描述】:

我有一个数字数组,我希望 tableview 显示我在搜索栏中输入的数字。这是我的代码,我不知道我应该在.subscribe987654322@之后的行中写什么

let dataSource = BehaviorRelay(value: [String]())
let disposeBag = DisposeBag()
var numbers = ["1", "2", "3", "4", "5"]

override func viewDidLoad() {
    super.viewDidLoad()
    
    dataSource.accept(numbers)
    
    searchBar.rx.text.orEmpty
        .throttle(.milliseconds(2000), scheduler: MainScheduler.instance)
        .filter{self.numbers.contains($0)}
        .subscribe(onNext: 
            
        })
    self.dataSource
        .asObservable()
        .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: TableViewCell.self)) {(row, element, cell) in
            cell.textLabel?.text = element
        }
        .disposed(by: disposeBag)

可能因为其他原因无法使用,还是谢谢大家的帮助

【问题讨论】:

    标签: ios swift uitableview uisearchbar rx-swift


    【解决方案1】:

    更新:

    您希望将响应式代码视为因果链。发生一些副作用会导致一些其他副作用。当然因果之间你有业务逻辑。

    首先请注意,不需要主题。在Intro To Rx这本书中我们了解到:

    主题提供了一种在 Rx 周围闲逛的便捷方式,但不建议将它们用于日常使用。

    现在我们在思考“因果关系”……我们想要达到的效果是什么?那将是表格视图的输出:

    • 每当用户点击添加按钮时添加一个数字,
    • 每当用户点击删除按钮时删除最后一个数字,
    • 并在用户在文本中输入数字时过滤列表 场...

    这是三个原因以及定义原因如何影响结果的逻辑。

    final class Example: UIViewController {
        var searchBar: UISearchBar!
        var addButton: UIButton!
        var deleteButton: UIButton!
        var tableView: UITableView!
        let disposeBag = DisposeBag()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            enum Input {
                case add
                case delete
            }
    
            let initial = ["1", "2", "3", "4", "5"] // this is what we start with
            Observable.merge(
                addButton.rx.tap.map { Input.add }, // flag addButton taps for adding a number
                deleteButton.rx.tap.map { Input.delete } // flag deleteButton taps for deleting a number
            )
            .scan(into: initial) { state, input in // setup a state machine that:
                switch input {
                case .add:
                    state.append("\(state.count + 1)") // adds a number when the add event happens
                case .delete:
                    state.removeLast() // removes the last number when a delete event happens
                }
            }
            .withLatestFrom(searchBar.rx.text) { list, text in
                list.filter { $0 == text } // here we filter the list based on the value in the search bar
            }
            .startWith(initial) // show the initial values at start
            .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self)) { _, element, cell in
                cell.textLabel?.text = element
            }
            .disposed(by: disposeBag)
        }
    }
    

    您可能希望使过滤器比我上面的过滤器更复杂或更简单。为了使上述可测试,只需将闭包移出到单独的函数中,以便它们可以独立测试。

    【讨论】:

    • 谢谢@Daniel T。但问题仍未解决。问题是,我使用这个填充 tableView:yadi.sk/i/5fzqN8VBq4vPcg 我还有从 tableView 添加和删除项目的按钮:yadi.sk/i/ouNYUK1ToMlZjw 现在,当我的代码看起来像这样时,我不明白如何填充 tableView(你的最终示例对我来说看起来很难,我不明白一些行):yadi.sk/i/6paJuDhpeELT1Q 现在我需要再来一次。我有一组数字来填充 tableView。我可以从中添加和删除数字。如果我输入 3,我还希望 tableView 显示 (3, 13, 33)
    • 如果你改变了需求,代码就会改变,这并不奇怪......至于不理解的一些行,我建议你去每个让你感到困惑的运算符的定义并阅读文档。有机会我会发布一个更新的示例。
    • 当用户在搜索栏中输入数字时,文本视图会发生什么?
    • 文本视图?也许是表格视图?表格视图必须仅显示数组中的那些数字,其中包括来自搜索栏的数字。所以,定期搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多