How do I do to update a UITableViewCell using RxSwift?

I studied example in a demo to create a UITableView and display cells.

In my opinion, items is a viewModel, I want to request some data over the network using Alamofire or another library. When I get the answer, how can I update the corresponding cell text?

In other words, I want to bind viewModel to cells. When the model data changed, the contents of the cells could be changed automatically.

I have an idea: create an Observable sequence for the contents of the cell (cell binding). When the server response data, it calls the tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top) function. But this does not seem to be grace or a good method.

So, hope some can help me :)

 let items = Observable.just([ "First Item", "Second Item", "Third Item" ]) items .bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) { (row, element, cell) in cell.textLabel?.text = "\(element) @ row \(row)" /* to do some binding or something else ? */ } .addDisposableTo(disposeBag) tableView .rx_modelSelected(String) .subscribeNext { value in DefaultWireframe.presentAlert("Tapped `\(value)`") } .addDisposableTo(disposeBag) 
+6
source share
3 answers

Your client (Alomofire, ...) will have something like this:

 func searchSomething() -> Observable<[YourModel]> 

In viewModel you will have the items property:

 private(set) lazy var items : Observable<[YourModel]> = self.client.searchSomething() .observeOn(MainScheduler.instance) .shareReplay(1) 

And then in viewController :

 tableView.dataSource = nil // Bind the items to the table view data source viewModel.items .bindTo(tableView.rx.items) { tableView, index, item in let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") cell.textLabel?.text = item.title // if item.title is a String return cell } .addDisposableTo(disposeBag) 
+4
source

you can try this

 //important! var items = Variable([YourModel]()) //empty now let cellIdentifier = NSStringFromClass(YourCell.self).components(separatedBy: ".")[1] tableView.register(YourCell.classForCoder(), forCellReuseIdentifier: cellIdentifier) _ = tableView.rx.setDelegate(self) items.asObservable().bindTo(tableView.rx.items(cellIdentifier: cellIdentifier, cellType: YourCell.self)){ (index,model,cell) in cell.detailTextLabel?.text = model.age }.addDisposableTo(dispose) //just like your network request DispatchQueue.main.asyncAfter(deadline: .now() + 3) { self.items.value = [1,3,4,5,6,7,8] //has value now } //it works fine for me 
0
source

change to variable and change value

 let items = Variable([ "First Item", "Second Item", "Third Item" ]) items .bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) { (row, element, cell) in cell.textLabel?.text = "\(element) @ row \(row)" /* to do some binding or something else ? */ } .addDisposableTo(disposeBag) tableView .rx_modelSelected(String) .subscribeNext { value in DefaultWireframe.presentAlert("Tapped `\(value)`") items.value = ["item 1", "item 2"] //your updated array } .addDisposableTo(disposeBag) 
-1
source

All Articles