[英]How should I do to update UITableViewCell by using RxSwift?
I learned example in the demo to create a UITableView and render cells. 我了解到例如在演示中创建一个UITableView和渲染单元。
In my opinion, items
is viewModel, I want to request some data across network by using Alamofire or other library. 我认为
items
是viewModel,我想使用Alamofire或其他库在网络上请求一些数据。 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. 换句话说,我想将viewModel绑定到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)
. 我有一个想法:为单元格的内容创建一个Observable序列(绑定到单元格)。当服务器响应数据时,它调用函数
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)
Your client
(Alomofire, ...) will have some like this: 您的
client
(Alomofire,...)将具有以下内容:
func searchSomething() -> Observable<[YourModel]>
In the viewModel
you will have the items
property: 在
viewModel
您将拥有items
属性:
private(set) lazy var items : Observable<[YourModel]> =
self.client.searchSomething()
.observeOn(MainScheduler.instance)
.shareReplay(1)
And then, in the viewController
: 然后,在
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)
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
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.