[英]Reload Tableview using RxSwift
我發現了這個問題。 我的陣列沒有正確更新。 我做了以下更改。
聲明ModelClass的dataSource
變量:
let dataSource = Variable<[SearchResult]>([])
現在將它與表視圖綁定為空:
dataSource.asObservable().bindTo(ResultsTable.rx.items(cellIdentifier: "SearchCell")){ row,Searchplace,cell in
if let C_cell = cell as? SearchTableViewCell{
C_cell.LocationLabel.text = Searchplace.place
}
}.addDisposableTo(disposeBag)
然后將包含searchPlaces的更新數組存儲在其中:
dataSource.value = self.array
現在,每次更改dataSource的值時,將重新加載表視圖。
避免使用“變量”,因為此概念將從RxSwift棄用,但官方遷移路徑尚未確定。
REF: https : //github.com/ReactiveX/RxSwift/issues/1501
因此,建議使用RxCocoa.BehaviorRelay。
let dataSource = BehaviorRelay(value: [SearchResultModel]())
綁定到tableView
self.dataSource.bind(to: self.tableView.rx.items(cellIdentifier: "SearchCell", cellType: SearchCell.self)) { index, model, cell in
cell.setupCell(model: model)
}.disposed(by: self.disposeBag)
獲取數據后:
let newSearchResultModels: [SearchResultModel] = ..... //your new data
dataSource.accept(newSearchResultModels)
希望這可以幫助:)
讓
array = Variable<[SearchResult]>([])
每當您點擊API時,將獲取的結果放在self.array.value
,它將自動更新。
self.array.asObservable().bindTo(ResultsTable.rx.items(cellIdentifier: "SearchCell", cellType:SearchCell.self))
{ (row, element, cell) in
cell.configureCell(element: element)
}.addDisposableTo(disposeBag)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.