Reputation: 745
I learned example in the demo to create a UITableView and render cells.
In my opinion, items
is viewModel, I want to request some data across network by using Alamofire or other library. When I get the response, How can I update the cell's text relevant?
In the other words, I want to bind viewModel to Cells. When the model's data changed, cell's content could changed automatically.
I have a idea is: create a Observable sequence for the cell's content (bind to cell).When the server response data, it call function tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
. But this seems not a grace or 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)
Upvotes: 7
Views: 8371
Reputation: 437
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
Upvotes: 0
Reputation: 2326
Your client
(Alomofire, ...) will have some like this:
func searchSomething() -> Observable<[YourModel]>
In the 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 the 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)
Upvotes: 5
Reputation: 405
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)
Upvotes: 0