简体   繁体   English

使用Swift自动重新排序核心数据中的UICollectionView

[英]Automatically Reordering UICollectionView in Core Data with Swift

I have an app that uses a UICollectionView and when a user taps and holds on a cell it allows the user to shift around and reorder the position of that cell (similar to how iOS allows you to reorder apps on your home screen). 我有一个使用UICollectionView的应用程序,当用户点击并保持在单元格上时,它允许用户转移并重新排序该单元格的位置(类似于iOS允许您重新排序主屏幕上的应用程序的方式)。

Of course the change in order is not saved and it goes back to its old order when you leave the view and come back. 当然,订单的更改不会保存,当您离开视图并返回时,它会返回到旧订单。 I was under the impression there is way to automatically save a new order of cells in a UITableView without explicitly writing everything out in Core Data. 我的印象是有一种方法可以在UITableView中自动保存新的单元格顺序,而无需在Core Data中明确写出所有内容。

How can I do that in a UICollectionView? 我怎么能在UICollectionView中做到这一点? Or is that not possible and I will have to write everything to Core Data manually? 或者这是不可能的,我将不得不手动将所有内容写入Core Data?

Edit: Current Pertinent Code: 编辑:当前相关代码:

@IBOutlet weak var groupCollection: UICollectionView!
var longPressGesture : UILongPressGestureRecognizer?
var newGroupOrderNum : Int?
let indexPath : IndexPath = []
var aryGroup : NSMutableArray!

func handleLongGesture(gesture: UILongPressGestureRecognizer) {

    switch(gesture.state) {

    case UIGestureRecognizerState.began:
        guard let selectedIndexPath = self.groupCollection.indexPathForItem(at: gesture.location(in: self.groupCollection)) else {
            break
        }
        groupCollection.beginInteractiveMovementForItem(at: selectedIndexPath)
    case UIGestureRecognizerState.changed:
        groupCollection.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
    case UIGestureRecognizerState.ended:
        groupCollection.endInteractiveMovement()
        var timestamp: Date {
            return Date()
        }
        let creationTime = timestamp

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let entity =  NSEntityDescription.entity(forEntityName: "Group", in:managedContext)
        let group = NSManagedObject(entity: entity!, insertInto: managedContext)

        let orderChangeGroup = aryGroup.object(at: indexPath.row)
        group.setValue(orderChangeGroup, forKey: "groupOrderNum")

        do {
            try managedContext.save()
        }
        catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }

        group.setValue(creationTime, forKey: "creationTime")
    default:
        groupCollection.cancelInteractiveMovement()
    }
}

I think you need to manually save it to Core Data after the ordering done: 我认为您需要在订购完成后手动将其保存到Core Data:

let context = self.fetchedResultsController.managedObjectContext
let event = self.fetchedResultsController.object(at: indexPath)
event.ordering = indexPath.row
do { try context.save() }
catch { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM