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

标签 ios swift uicollectionview childviewcontroller

我有一个 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/

相关文章:

ios - 如何在动态类型大小更改时更新 WKWebView?

ios - Example-Swift.h 未找到

ios - 在segue中添加数据运行时将无法识别SecondViewController类

ios - 识别何时将 UIView 拖动到 UICollectionViewCell 顶部

ios - 如何使用 Swift 在经过一定时间后删除 map 图钉?

android - apache cordova 应用程序中的通知处理

ios - 在 Swift 中页面 View Controller 内的 View Controller 之间传递数据

swift - Swift 字符串中区分大小写的字符替换

ios - 如何在 JSQMessagesViewController 中使消息气泡居中

ios - 更改 UICollectionViewCell 中的标签位置