在Swift的Collection View中重新排序单元格

时间:2018-02-21 07:03:11

标签: uicollectionview swift4

我使用集合视图,在单元格内有一个图像视图。图片来自画廊,现在我试图重新排序我的图片。假设当我将索引1处的图像移动到索引0时它移动得很好但问题是我在imageview中的图像不是原始的索引1,它被更改为放置在索引0的图像。我有试过这段代码,

 longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
    uploadDocCollectionView.addGestureRecognizer(longPressGesture)
 @objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {

        switch(gesture.state) {

        case UIGestureRecognizerState.began:
            guard let selectedIndexPath = self.uploadDocCollectionView.indexPathForItem(at: gesture.location(in: self.uploadDocCollectionView)) else {
        break

        }
        uploadDocCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        case UIGestureRecognizerState.changed:
            uploadDocCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case UIGestureRecognizerState.ended:
        uploadDocCollectionView.endInteractiveMovement()
        default:
        uploadDocCollectionView.cancelInteractiveMovement()


       }
}

在我的收藏中,这些是我所称的功能,

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    print("Starting Index: \(sourceIndexPath.item)")
    print("Ending Index: \(destinationIndexPath.item)")
}

1 个答案:

答案 0 :(得分:1)

在选定索引处删除图像,然后将其插入同一数组的目标索引

ax.set_title(r'Globales Minimum $\hat \beta$ = {}'.format(roundi), fontsize=20)