Idan Yehuda
Idan Yehuda

Reputation: 562

Swipe Cell in UICollectionView - Swift

Is there an elegant and short way to progrematiclly swipe between two cells (assuming we have the desired NSIndexPath of the two cells)?

Upvotes: 0

Views: 872

Answers (1)

Dima Deplov
Dima Deplov

Reputation: 3718

I see few possibilities here, having the information you provide.

1) You can use standard UICollectionView method: - moveItemAtIndexPath:toIndexPath:, but you must update your data source first. For example, assume you already updated data source (note that this example code is useless until you figure out index changes after moving items.):

collectionView.performBatchUpdates({ () -> Void in
   collectionView.moveItemAtIndexPath(firstIndexPath,toIndexPath:secondIndexPath)
   //note: proper indexes might be changed after moveItem.. method. Play with it and you'll find the proper one for your item.
   collectionView.moveItemAtIndexPath(secondIndexPath,toIndexPath:firstIndexPath)        
}, completion: { (finish) -> Void in

})

2) You can recalculate your layout if you use custom layout

3) You can just reload collection view with reloadData or reloadItemsAtIndexPaths E.g.:

 var dataSourceArray = [1,2,3,4,5]
 // some event occurred -> dataSourceArray changed
 dataSourceArray = [1,2,5,4,3]
 // call collectionView.reloadData() or reloadItemsAtIndexPaths(_:).

If you'll use 1st or 3rd way, in both cases data source must be up to date.

Upvotes: 2

Related Questions