2016-02-03 10 views
10

Sắp xếp lại đang làm việc tại iOS9 khi tôi thêm video này vào UICollectionViewController lớp con tôiUICollectionViewController sắp xếp lại không hoạt động khi nhúng vào trong một cái nhìn chứa

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

Nó không hoạt động khi UICollectionViewController lớp con này được nhúng vào trong một cái nhìn chứa.

Tôi đã thực hiện một bản demo của vấn đề here

Bất kỳ ý tưởng về lý do tại sao hoặc làm thế nào để sửa chữa nó?

+0

Cập nhật ví dụ với giải pháp bởi @Scooter [ở đây] (https://github.com/jameszaghini/UICollectionViewController-reordering-problem/tree/fixed) –

Trả lời

13

Vấn đề ở đây là bạn đặt UICollectionView bên trong UIContainerView đang nằm bên trong UIViewController. Điều này chỉ đòi hỏi một vài bước nữa để UICollectionView hoạt động như mong đợi.

Thêm dòng sau vào viewDidLoad trong CollectionViewController của bạn:

self.collectionView!.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: "handleLongGesture:")) 

Sau đó, thêm chức năng sau đây để CollectionViewController của bạn:

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() 
    } 
} 

Cuối cùng chỉ cần đảm bảo để bao gồm những điều sau đây để đảm bảo bạn xử lý các nguồn dữ liệu chính xác:

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

Kiểm tra điều này link để biết thêm về điều này.

Hy vọng điều này sẽ giúp bạn.

9

Câu trả lời của xe tay ga là đúng! Đây là Swift phiên bản 3 cú pháp:

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. 
} 

Lưu ý rằng mã này không chiếm vị trí cảm ứng bù đắp - vì vậy di động của bạn sẽ 'nhảy' để được tập trung dưới ngón tay của bạn khi bạn bắt đầu kéo. Nếu bạn muốn ngăn chặn điều đó, thì bạn sẽ cần xác định trong tài sản UIViewController a CGPoint của bạn (có tên initialGestureLocationInCell trong mã bên dưới). Và sau đó thay thế trong ví dụ ban đầu với điều này:

[...] 
     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) 
[...] 
+1

Cám ơn cập nhật lên Swift 3.0! – Scooter

+1

Phần 2 thực sự hữu ích! Cảm ơn. – Nico

+0

Cảm ơn bạn rất nhiều vì lời khuyên liên quan đến việc bù đắp vị trí cảm ứng. – Storix

Các vấn đề liên quan