【问题标题】:UICollectionView Cells ReorderingUICollectionView 单元格重新排序
【发布时间】:2017-08-17 13:46:58
【问题描述】:

我正在尝试重新排序我的 CollectionView 单元格。但似乎无法通过 UILongPressGestureRecognizer。状态从开始 -> 更改 -> 结束。但它不会用到 moveItemAtIndexPath 函数。 这是我的代码:

  func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {

var temp1 = [self.viewThatIsShown.selectionForView?.pageArray.count]

let temp = temp1.removeAtIndex(sourceIndexPath.item)
temp1.insert(temp, atIndex: destinationIndexPath.item)
}
 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    let layout = self.viewThatIsShown.selectionForView!.rawJSON["pages"].arrayValue[section]["layout"].stringValue

    if layout == "video" || layout == "social-media" {
        return 1
    } else if layout == "pre-list" {
        let dataJSON = self.viewThatIsShown.selectionForView!.rawJSON["pages"].arrayValue[section]["data"]
        return dataJSON["listitems"].arrayValue.count
    }

    let dataSet = self.viewThatIsShown.dataForView!.dataArray[section]

    return min(dataSet.embeddedItemsArray.count, 2)

}
 switch(gesture.state) {

    case UIGestureRecognizerState.Began:
        guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
            break
        }
        collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
    case UIGestureRecognizerState.Changed:
        collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
    case UIGestureRecognizerState.Ended:
        collectionView.endInteractiveMovement()
    default:
        collectionView.cancelInteractiveMovement()
    }

也没有显示错误。我想我在计算部分中的项目数量时犯了一些错误。但是想不通。 P.S 我的 UICollectionview 中有六个部分,我想将其用于拖放部分。

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    查看 UILongPressGestureRecognizer 选择器的实现会非常有帮助,因为这是拼图的关键部分。不过,一般来说,您需要观察 .began、.changed、.ended 状态,正如您所提到的,并更新位置。一种方法是打开gesture.state 并执行以下操作:

    switch gesture.state {
    case .began:
        //Get a reference to the indexPath being pressed
        guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else { break }
        //Start interactive movement
        self.collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
    case .changed:
        //Update position based on new gesture location
        self.collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: self.collectionView))
    case .ended:
        //End interaction with cell. This will cause moveItemAtIndexPath to fire.
        self.collectionView.endInteractiveMovement()
    case .default:
        //Just to cover your bases
        self.collectionView.cancelInteractiveMovement()
    }
    

    【讨论】:

    • 问题是moveItemAtIndexPath 函数根本没有被执行,还是它什么也没做?该函数内的代码实际上什么都不做 - 它创建一个包含单个可选Int 的数组,删除该值,然后将值放回原处。那里的任何内容都不会影响您的 collectionView 或它的数据源。
    • 嗨,是的,collectionView 正在移动。但是不,我想移动我也拥有的 ReusableColectionCells。可以移动它们吗?我的 CollectionView 中有 6 个部分。我想移动那些。有可能吗?