gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:07 24 4
gpt4 key购买 nike

关注Documentation之后RxDataSources 我无法让它工作。

当我单击 CollectionViews 的一个元素时,它被删除,如我的代码所示,但 View 上没有任何反应,尽管我的 sections[0].items 上少了一个元素。我认为我在将数据源与 View 绑定(bind)时做错了,但我无法弄清楚。

    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)

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

有人知道发生了什么吗?

提前致谢。

最佳答案

这是因为您的 sections Array 就是一个 ArrayObservable.just(sections) 不会仅仅因为您修改了 rx_itemSelected 订阅中的 sections 就发送另一个元素。您需要将您的数据源绑定(bind)到一个 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)

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

关于ios - 使用 RxDataSources 在 UICollectionView 中删除的项目没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com