使用 RxDataSources 在 UICollectionView 中删除的项目没有动画

Not animation on item removed in UICollectionView with RxDataSources

在遵循 RxDataSourcesDocumentation 之后,我无法让它工作。

当我单击 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([=10=].row)
    }.addDisposableTo(disposeBag)

视图已完美加载所有初始类别,但当我删除其中一个时,视图未刷新。

有人知道发生了什么事吗?

提前致谢。

因为你的 sections Array 就是一个 ArrayObservable.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([=10=].row)
    data.value = d
}.addDisposableTo(disposeBag)

但是,无论如何我都会推荐一个更强大的解决方案。 Use this example.