Cannot reorder View collection cells

I have a Split View controller, and when a user clicks a cell in a table view, the application shows a view controller with a container view and a segment control to switch between two child view controllers.

The first child view controller is the collection view controller. Each cell in the Collection View has a word.

The view is as follows:

view screen

I add a child view controller programmatically using this code (from the detailed view controller, Subviews container):

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

Here is the code to switch between two child views with a segmented control:

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

To reorder the cells, I applied this method to my own 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() } 

The problem is that when I click on the Table View Cell element and the application shows the Container View with the built-in collection controller built in, I cannot change the order of the cells, and to be able to do this I need to change the Child (using Segmented Control) , and then return to the collection view.

How can I solve this problem?

+6
source share
2 answers

Try implementing these methods:

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

You can follow this guide: http://nshint.io/blog/2015/07/16/uicollectionviews-now-have-easy-reordering/

0
source

I would check two things:

First you call willMoveToParentViewController in your cycle(from: UIViewController, to: UIViewController) method cycle(from: UIViewController, to: UIViewController) , but not in viewDidLoad , try to do this. From what I can read, you can call cycle in your viewDidLoad method, calling the method in a Nil object ( from ) does nothing.

Secondly, check if your WordCollectionViewController collectionView delegate and dataSource when it loads for the first time. This can be difficult sometimes when loading from StoryBoard if the items are not initialized in time.

0
source

All Articles