无法重新排序集合视图单元格

Cannot reorder Collection View cells

我有一个拆分视图控制器,当用户单击 Table 视图中的一个单元格时,该应用会显示一个带有容器视图的视图控制器和一个用于在两个子视图控制器之间切换的段控件。

第一个子视图控制器是一个集合视图控制器。在 Collection View 的每个单元格中都有一个词。

视图如下所示:

我使用此代码(从详细视图控制器,子视图的容器)以编程方式添加子视图控制器:

override func viewDidLoad() {


    self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "WordCollectionViewControllerID")
    (self.currentViewController as! WordCollectionViewController).text = self.text
    self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false
    self.addChildViewController(self.currentViewController!)
    self.addSubview(subView: self.currentViewController!.view, toView: self.textContainerView)
    self.currentViewController?.view.layoutIfNeeded()
    self.currentViewController?.didMove(toParentViewController: self)

    super.viewDidLoad()

}

func addSubview(subView: UIView, toView parentView: UIView) {
    parentView.addSubview(subView)

    var viewBindingsDict = [String: AnyObject]()
    viewBindingsDict["subView"] = subView
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: [], metrics: nil, views: viewBindingsDict))
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: [], metrics: nil, views: viewBindingsDict))
}

下面是使用分段控件在两个子视图之间切换的代码:

@IBAction func changeChildViewController(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "WordCollectionViewControllerID")
        (newViewController as! WordCollectionViewController).text = self.text
        newViewController!.view.translatesAutoresizingMaskIntoConstraints = false
        cycle(from: self.currentViewController!, to: newViewController!)
        self.currentViewController = newViewController
        print("Reorder")
    } else {
        let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "NavigationControllerEditTextViewControllerID")
        (newViewController?.childViewControllers[0] as! EditTextViewController).text = self.text
        newViewController!.view.translatesAutoresizingMaskIntoConstraints = false
        cycle(from: self.currentViewController!, to: newViewController!)
        self.currentViewController = newViewController
        print("Edit")
    }
}

func cycle(from: UIViewController, to: UIViewController) {
    from.willMove(toParentViewController: nil)
    self.addChildViewController(to)

    self.addSubview(subView: to.view, toView: self.textContainerView)

    to.view.layoutIfNeeded()

    from.view.removeFromSuperview()

    from.removeFromParentViewController()
    to.didMove(toParentViewController: self)
}

为了能够重新排序单元格,我已经为我的自定义 CollectionViewController 实现了这个方法 class:

override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    // Swap values if sorce and destination
    let change = textArray[sourceIndexPath.row]


    textArray.remove(at: sourceIndexPath.row)
    textArray.insert(change, at: destinationIndexPath.row)


    // Save changes in Core Data
    text?.reordered_text = textArray.joined(separator: " ")

    (UIApplication.shared.delegate as! AppDelegate).saveContext()


    collectionView.reloadData()
}

问题是,当我单击 Table View Cell 并且应用程序显示嵌入了 Collection View Controller 的 Container View 时,我无法对单元格重新排序,为了能够做到这一点,我必须更改子项(使用分段控件)然后 return 到集合视图。

我该如何解决这个问题?

我会检查两件事:

首先你在你的cycle(from: UIViewController, to: UIViewController)方法中调用willMoveToParentViewController,而不是在viewDidLoad中,尝试这样做。 据我所知,您应该能够在 viewDidLoad 方法中调用 cycle,在 Nil 对象 (from) 上调用方法什么都不做。

其次检查您的 WordCollectionViewController collectionView delegatedataSource 在第一次加载时是否设置正确。如果元素没有及时初始化,从 StoryBoard 加载时,这些有时会很棘手。

尝试实现这些方法:

func beginInteractiveMovementForItem(at: IndexPath) -> Bool
func updateInteractiveMovementTargetPosition(targetPosition: CGPoint)
func endInteractiveMovement()
func cancelInteractiveMovement()

您可以按照本教程进行操作:http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/