Reload Tableview using RxSwift

☆樱花仙子☆ 提交于 2019-12-03 20:24:34
Mehreen

I have found the issue. My array was not being getting updated correctly. I did the following changes.

Declare dataSource variable of ModelClass:

let dataSource = Variable<[SearchResult]>([])

Bind it with the table view right now it is empty:

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)

Then store my updated array in it that contains the searchPlaces:

dataSource.value = self.array

Now each time when value of dataSource will be changed, table view will be reloaded.

Avoid using "Variable" because of this concept will be deprecated from RxSwift but official migration path hasn't been decided yet.

REF: https://github.com/ReactiveX/RxSwift/issues/1501

Hence, recommend using RxCocoa.BehaviorRelay instead.

let dataSource = BehaviorRelay(value: [SearchResultModel]())

Bind to 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)

after fetch data:

let newSearchResultModels: [SearchResultModel] = ..... //your new data
dataSource.accept(newSearchResultModels)

Hope this can help :)

Let

array = Variable<[SearchResult]>([])

Whenever you hit your API, put your fetched results in self.array.value and it will automatically gets updated.

 self.array.asObservable().bindTo(ResultsTable.rx.items(cellIdentifier: "SearchCell", cellType:SearchCell.self)) 
   { (row, element, cell) in
        cell.configureCell(element: element)
   }.addDisposableTo(disposeBag)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!