ios - RXSwift - RxCollectionViewSectionedReloadDataSource - 拖放移动(重新排序)
问题描述
我曾经RxCollectionViewSectionedReloadDataSource
将我的数据加载到UICollectionView
.
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, WorkGroup>>(
configureCell: { (_, collectionView, indexPath, item) in
guard let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: WorkGroupCell.identifier, for: indexPath) as? WorkGroupCell else {
return WorkGroupCell()
}
cell.viewModel = item
return cell
}
)
viewModel.items.bind(to: tileCollectonView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
tileCollectonView.rx.setDelegate(self).disposed(by: disposeBag)
使用上面的代码我可以显示数据。但我想拖放(重新排序)单元格。
请让我知道如何使用RxSwift
.
帮助将不胜感激。
解决方案
通常情况下,您需要一个状态机。
涉及的逻辑非常简单:
struct State<Item> {
var items: [Item] = []
}
func state<Item>(initialState: State<Item>, itemMoved: Observable<ItemMovedEvent>) -> Observable<State<Item>> {
itemMoved
.scan(into: initialState) { (state, itemMoved) in
state.items.move(from: itemMoved.sourceIndex.row, to: itemMoved.destinationIndex.row)
}
.startWith(initialState)
}
extension Array
{
mutating func move(from oldIndex: Index, to newIndex: Index) {
if oldIndex == newIndex { return }
if abs(newIndex - oldIndex) == 1 { return self.swapAt(oldIndex, newIndex) }
self.insert(self.remove(at: oldIndex), at: newIndex)
}
}
以上使用ItemMovedEvent
RxCocoa 提供的表视图。为了为集合视图创建类似的东西,您需要将其包装UICollectionViewDragDelegate
在委托代理中。可以在此处找到有关如何执行此操作的文章:将 Swift 委托转换为 RxSwift 可观察对象
推荐阅读
- xamarin.forms - 数据访问库和 xamarin 模型类之间发生隐式转换错误
- vue.js - 过滤查询分页
- jmeter - Jmeter每秒增加请求
- c# - 如何使用 OPC.UA.PubSub 库进行身份验证
- azure - 通过 terraform 有条件地添加到资源属性
- angular-material - 使用tailwindcss设置输入类型=数字时,如何删除matInput标签周围的轮廓
- vue.js - 单击事件不适用于 ApexCharts (Vue3)
- java - 带有系统变量的 WSO2 JMS 地址端点 uri
- c++ - C++算法库如何检查输出的范围,当它小于输入的范围时不创建段错误?
- javascript - 通过正则表达式选择类名,除了一些