Reputation: 1257
I am new to RxSwift and working on some sample to test. I have show some data on uitableview with the help of RxSwift. However when I try to delete any item from tableview and reload. Observable array didn't update and due to this scrolling at last item crashes. Below is the code, please help me know what I am doing wrong.
self.itemArray = NSMutableArray(objects: "First Item","Second Item","Third Item","Fourth Item","Fifth Item","Sixth Item","Seventh Item","Eight Item","Nineth Item","Tenth Item","Eleventh Item","Twelveth Item","Thirtheenth Item","Fourtheenth Item","Fifteenth Item","Sixteenth Item","Seventhenth Item","First Item","Second Item","Third Item","Fourth Item","Fifth Item","Sixth Item","Seventh Item","Eight Item","Nineth Item","Tenth Item","Eleventh Item","Twelveth Item","Thirtheenth Item","Fourtheenth Item","Fifteenth Item","Sixteenth Item","Seventhenth Item")
var seqValue = Observable.just(self.itemArray)
seqValue
.bind(to: rxtableView.rx.items) { (tableView, row, seqValue) in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = self.itemArray.object(at: row) as? String
cell.backgroundColor = .clear
return cell
}.disposed(by: disposeBag)
self.rxtableView.rx.itemDeleted
.subscribe(onNext: { [unowned self]indexPath in
self.itemArray.removeObject(at: indexPath.row)
seqValue = Observable.just(self.itemArray)
self.rxtableView.reloadData()
}).addDisposableTo(disposeBag)
Upvotes: 1
Views: 4499
Reputation: 1391
I think what you want is RxSwift's Variable
:
let seqValue = Variable<String>(["First Item", ...])
so you in your .itemDeleted
you can modify that variable:
seqValue.value.remove(at: indexPath.row)
Variable
is an Observable
, so changes are propagated automatically. No need to call reloadData()
.
Upvotes: 0
Reputation: 4077
make it BehaviorSubject, i.e. -
var seqValue = BehaviorSuject.create(self.itemArray)
and instead of 'seqValue = Observable.just(self.itemArray)' do
seqValue.onNext(self.itemArray)
calling '.reloadData()' is not necessary
Upvotes: 1