嵌入容器视图时,UICollectionViewController重新排序不起作用

Jam*_*ini 13 ios uicollectionview swift ios9

当我将它添加到我的UICollectionViewController子类时,重新排序在iOS9中工作

override func collectionView(collectionView: UICollectionView, 
        moveItemAtIndexPath sourceIndexPath: NSIndexPath, 
           toIndexPath destinationIndexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)

当此UICollectionViewController子类嵌入容器视图时,它不起作用.

我在这里做了一个问题的演示

关于为什么或如何修复它的任何想法?

Sco*_*ter 16

这里的问题是你将UICollectionView放在一个驻留在UIViewController中的UIContainerView中.这需要更多步骤才能使UICollectionView按预期工作.

将以下内容添加到CollectionViewController中的ViewDidLoad:

    self.collectionView!.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: "handleLongGesture:"))
Run Code Online (Sandbox Code Playgroud)

然后将以下函数添加到CollectionViewController:

    func handleLongGesture(gesture: UILongPressGestureRecognizer)
    {

    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()
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,请确保包含以下内容以确保正确处理dataSource:

    override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {
    // Swap the values of the source and destination
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看此链接.

希望这能帮到你.


Nik*_*iev 15

滑板车的答案是正确的!这是Swift 3语法版本:

import UIKit

class ViewController: UIViewController
{
    // MARK: - IBOutlets
    @IBOutlet weak var uiCollectionView: UICollectionView!

    // MARK: - Lifecycle
    override func viewDidLoad()
    {
        super.viewDidLoad()

        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongGesture))
        self.uiCollectionView.addGestureRecognizer(longPressGesture)
    }


    // MARK: - Gesture recogniser
    @objc func handleLongGesture(gesture: UILongPressGestureRecognizer)
    {
        switch(gesture.state)
        {

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

            self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)

        case .changed:
            self.uiCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))

        case .ended:
            self.uiCollectionView.endInteractiveMovement()

        default:
            self.uiCollectionView.cancelInteractiveMovement()
        }
    }
}


// MARK: - UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        // TODO: Link to your data model
        return 20
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        // TODO: Link to your data model
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
        return cell
    }


    func collectionView(_ collectionView: UICollectionView,
                        moveItemAt sourceIndexPath: IndexPath,
                        to destinationIndexPath: IndexPath)
    {
        // TODO: Update your data model to reflect the change
    }
}


// MARK: - UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate
{
    // TODO: Add any UICollectionViewDelegate here if needed.
}
Run Code Online (Sandbox Code Playgroud)

请注意,此代码不考虑触摸位置偏移 - 因此当您开始拖动时,您的单元格将"跳转"以在您的手指下居中.如果你想阻止它,那么你需要在你UIViewControllerCGPoint属性中定义(initialGestureLocationInCell在下面的代码中命名).然后用最初的例子代替:

[...]
        case .began:
        guard let selectedIndexPath = self.uiCollectionView.indexPathForItem(at: gesture.location(in: self.uiCollectionView)) else
        {
            break
        }

        let selectedCell = self.uiCollectionView.cellForItem(at: selectedIndexPath)!
        let gestureLocationInCell_RelativeToOrigin = gesture.location(in: selectedCell)
        let gestureLocationInCell_RelativeToCentre = CGPoint(x: gestureLocationInCell_RelativeToOrigin.x - selectedCell.frame.size.width/2,
                                                             y: gestureLocationInCell_RelativeToOrigin.y - selectedCell.frame.size.height/2)

        self.initialGestureLocationInCell = gestureLocationInCell_RelativeToCentre
        self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)

    case .changed:
        let gestureLocationInCollectionView = gesture.location(in: gesture.view!)
        let targetPosition = CGPoint(x: gestureLocationInCollectionView.x - self.initialGestureLocationInCell.x,
                                    y: gestureLocationInCollectionView.y - self.initialGestureLocationInCell.y)

        self.uiCollectionView.updateInteractiveMovementTargetPosition(targetPosition)
[...]
Run Code Online (Sandbox Code Playgroud)

  • 第二部分非常有用!谢谢. (4认同)