I have used RxCollectionViewSectionedReloadDataSource
to load my data into 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)
Using above code i can display the data. But i want to drag and drop(Reorder) the cell.
Please let me know how can i do that reorder using RxSwift
.
Help will be highly appreciated.
2
Answers
As is often the case, you need a state machine.
The logic involved is quite simple:
The above uses the
ItemMovedEvent
provided by RxCocoa for table views. In order to create something like that for a collection view, you will need to wrap theUICollectionViewDragDelegate
in a delegate proxy. An article on how to do that can be found here: Convert a Swift Delegate to RxSwift Observablesso I assume that you are using
RxDataSources
after you set datasource variable, you need to set two more properties:
canMoveItemAtIndexPath
,moveItem
and use
UICollectionView
methods to start/update/end interaction.in my case I added long press gesture to handle dragging
then you can drag your cell
in
moveItem
closure, you should move actual data in your collection.