I have an tableview where I populate some data from view model, I am using RxSwift and RxDataSources... I have next code
enum TableViewEditingCommand {
case DeleteItem(IndexPath)
}
struct SectionedTableViewState {
fileprivate var sections: [MySectionViewModel]
init(sections: [MySectionViewModel]) {
self.sections = sections
}
func execute(command: TableViewEditingCommand) -> SectionedTableViewState {
switch command {
case .DeleteItem(let indexPath):
var sections = self.sections
var items = sections[indexPath.section].items
items.remove(at: indexPath.row)
sections[indexPath.section] = MySectionViewModel(original: sections[indexPath.section], items: items)
return SectionedTableViewState(sections: sections)
}
}
}
my view controller has something like this
class MyViewController : UIViewController {
let dataSource = RxTableViewSectionedAnimatedDataSource<MySectionViewModel>()
override func viewDidLoad() {
super.viewDidLoad()
viewModel.findElements()
.bindTo(tableView.rx.items(dataSource:dataSource))
.addDisposableTo(disposeBag)
dataSource.configureCell = { datasource, tableView, indexPath, item in
.... my configuration
}
dataSource.canEditRowAtIndexPath = { _ in true}
let deleteCommand = tableView.rx.itemDeleted.asObservable()
.map(TableViewEditingCommand.DeleteItem)
let initialState = SectionedTableViewState(sections:dataSource.sectionModels)
Observable.of(deleteCommand)
.merge()
.scan(initialState) { (state: SectionedTableViewState, command: TableViewEditingCommand) -> SectionedTableViewState in
return state.execute(command: command)
}
.startWith(initialState)
.map {
$0.sections
}
.shareReplay(1)
.bindTo(tableView.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
}
}
seems like in this line
let initialState = SectionedTableViewState(sections:dataSource.sectionModels)
sections is an empty array, because I get an index out range exception and when I print array, it has not any elements. My question is how can I get sections from datasource?
I've seen the example from this url: https://github.com/RxSwiftCommunity/RxDataSources/blob/master/Example/Example3_TableViewEditing.swift
Thanks in advance.