Moving Cells in UICollectionView

Asked 2 years ago, Updated 2 years ago, 94 views

I'd like to drag the cells in the collection view.
In the case of table view, I think you can move rows in edit mode, but does the collection view have such a function?

To the UICollectionViewDataSource protocol

 ·public func collectionView (collectionView: UICollectionView, canMoveItemAtIndexPathindexPath:NSIndexPath) ->Bool
·public function collectionView (collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath)

Also, in the UICollectionView

 ·public func moveItemAtIndexPath(indexPath:NSIndexPath, toIndexPath newIndexPath:NSIndexPath)

There seems to be a method like this, but can I move cells using these methods?

I would appreciate it if you could lend me some advice.

swift xcode uicollectionview

2022-09-30 21:16

1 Answers

I think this site will be helpful.
UICollectionViews Now Have Easy Reordering

As for the English site, you can also drop the sample project from GitHub, and you can roughly understand how to do it by reading the actual code.

(1) Using the UICollectionViewController
Basically, just implement collectionView(_:moveItemAtIndexPath:toIndexPath:).

(2) When using your own ViewController
In addition to the above, you will need to use the GestureRecoginizer to write down the drag start/state change/end process.

(3) If you want to use more custom layout with your ViewController
In addition to the above, I will override some of the methods in my Layout class.

One thing to note is that it is a feature of iOS9 or later, so it is necessary to check if the application under development should be compatible only with iOS9 or later.If there is no problem, I think you should try the sample code.


2022-09-30 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.