繁体   English   中英

UICollectionView单元重新排序

[英]UICollectionView Cells Reordering

我正在尝试重新排序我的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()
    }

也没有显示错误。 我想在计算Section中的项目数时我犯了一些错误。 但无法弄清楚。 PS我在UICollectionview中有六个部分,我想用它来拖放部分。

看到你的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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM