gpt4 book ai didi

ios - 无法重新排序 Collection View 单元格

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:33 24 4
gpt4 key购买 nike

我有一个 Split View Controller,当用户单击 Table View 中的一个单元格时,该应用会显示一个 View Controller,其中包含一个 Container View 和一个 Segment Control,用于在两个 subview Controller 之间切换。

第一个 subview Controller 是一个 Collection View Controller 。在 Collection View 的每个单元格中都有一个词。

View 看起来像这样:

view screen

我使用此代码(从详细 View Controller , subview 的容器)以编程方式添加 subview Controller :

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

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

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

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

我该如何解决这个问题?

最佳答案

尝试实现这些方法:

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/

关于ios - 无法重新排序 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39472133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com