2017-08-24 192 views
0

我是RxSwift的新手,並且需要測試一些樣本。我在RxSwift的幫助下顯示了一些關於uitableview的數據。但是,當我嘗試從tableview中刪除任何項目並重新加載。 Observable數組沒有更新,並且由於在最後一項崩潰時滾動。下面是代碼,請幫助我知道我做錯了什麼。RxSwift中可觀察元素的數組

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) 

回答

1

使其BehaviorSubject,即 -

var seqValue = BehaviorSuject.create(self.itemArray) 

,而不是 'seqValue = Observable.just(self.itemArray)',並做

seqValue.onNext(self.itemArray) 

調用 '.reloadData()'沒有必要

+0

我改變了這一行let itemValue = BehaviorSubject.init(value:self.itemArray),它的工作 – Kashif

0

我想你想要的是RxSwift的Variable

let seqValue = Variable<String>(["First Item", ...]) 

讓你在你的.itemDeleted可以修改變量:

seqValue.value.remove(at: indexPath.row) 

VariableObservable,所以更改會自動傳播。無需撥打reloadData()

相關問題