Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift, RxCocoa and UITableview

I have a problem with implementing a UITableView using RxSwift.

I tried to bind an observable of an array of models to the table items with the following code. models.bind(to: self.tableView.rx.items(cellIdentifier: "Cell", cellType: ModelTableViewCell.self.

But when I do it gives me the following error: Type 'inout UITableView' does not conform to protocol 'ReactiveCompatible' and I know the error can't be right because NSObject extends ReactiveCompatible so UITableView also does. Also, my project code isn't really different than the examples shown on RxSwiftCommunity

I created a small example project that has the error.

[Example code showing the error (picture)]

like image 642
Martyn Avatar asked Oct 26 '25 12:10

Martyn


1 Answers

Swift is quite good language but sometimes happens moments when compiler couldn't recognize the type of parameters. Then you need to explicit define a type of arguments. In your case you need to define the type of block arguments, see the code:

func bindRx(viewModel: ViewModel) {
    viewModel.models.bind(to: tableView.rx.items(cellIdentifier: ModelTableViewCell.ReuseIdentifier,
                                                 cellType: ModelTableViewCell.self)) { (_, model: Model, cell: ModelTableViewCell) in
        cell.textLabel?.text = model.name
    }
    .addDisposableTo(disposeBag)
}

enter image description here

like image 198
Ihar Katkavets Avatar answered Oct 29 '25 02:10

Ihar Katkavets



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!