在遵循文档 of RxDataSources之后,我无法使它工作。
当我单击CollectionViews的一个元素时,它会被移除,正如我的代码所示,但是视图上什么也不会发生,尽管我的sections[0].items上有一个更少的元素。我认为我做了一些错误的绑定数据源与视图,但我无法弄清楚它。
let dataSource = RxCollectionViewSectionedAnimatedDataSource<SectionOfCategoryMO>()
private var e1cat = CatMngr.SI.getAlphabeticallyOrderedCategories(type: .e1)
dataSource.configureCell = { ds, tv, ip, item in
let cell = tv.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: ip) as! CategoryCollectionViewCell
cell.categoryName.text = item.identity.string
cell.categoryName.numberOfLines = 0
cell.categoryCircle.makeCircle()
cell.categoryCircle.backgroundColor = self.categoryColors[ip.row]
return cell
}
dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: .Fade, reloadAnimation: .Fade, deleteAnimation: .Automatic)
var sections = [SectionOfCategoryMO(header: "a", items: e1cat)]
Observable.just(sections)
.bindTo(myCollection.rx_itemsWithDataSource(dataSource))
.addDisposableTo(disposeBag)
myCollection.rx_itemSelected.subscribeNext{
sections[0].items.removeAtIndex($0.row)
}.addDisposableTo(disposeBag)视图完全包含所有初始类别,但当我删除其中一个类别时,视图不会刷新。
有人知道发生了什么吗?
提前谢谢。
发布于 2016-09-20 16:51:28
这是因为你的sections Array就是一个Array。Observable.just(sections)不会仅仅因为修改了rx_itemSelected订阅中的sections而发送另一个元素。您需要将数据源绑定到一个Observable,当事情发生变化时,它实际上会发送新的元素。
类似于:
let initialData = [SectionOfCategoryMO(header: "a", items: e1cat)]
let data = Variable<SectionOfCategoryMO>(initialData)
data.asObservable()
.bindTo(myCollection.rx_itemsWithDataSource(dataSource))
.addDisposableTo(disposeBag)
myCollection.rx_itemSelected.subscribeNext {
let d = data.value
d[0].items.removeAtIndex($0.row)
data.value = d
}.addDisposableTo(disposeBag)然而,无论如何,我会推荐一个更健壮的解决方案。用这个例子。
https://stackoverflow.com/questions/39599127
复制相似问题