我有一个拆分视图控制器,当用户单击表视图中的单元格时,该应用程序会显示一个带有容器视图和段控件的视图控制器,以在两个子视图控制器之间切换。
第一个子视图控制器是Collection View Controller。在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类实现此方法的单元格重新排序:
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()
}
问题是,当我单击表视图单元格并且应用程序显示嵌入了集合视图控制器的容器视图时,我无法对单元格进行重新排序,为了能够执行此操作,我必须更改子(使用分段控件)然后返回到集合视图。
我该如何解决这个问题?
答案 0 :(得分:0)
我会检查两件事:
首先,您在willMoveToParentViewController
方法中调用cycle(from: UIViewController, to: UIViewController)
,但不在viewDidLoad
中,请尝试执行此操作。
根据我的内容,您应该可以在cycle
方法中调用viewDidLoad
,在Nil
上调用方法 - 对象(from
)不会执行任何操作。
其次,检查第一次加载时WordCollectionViewController
collectionView delegate
和dataSource
是否设置正确。如果元素未及时初始化,从StoryBoard
加载时,这些可能会很棘手。
答案 1 :(得分:0)
尝试实施这些方法:
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/