简体   繁体   中英

Rearranging order of TableView cells using Core Data

I have a custom UITableView class that handles animations for table view, and have tested the following code with an Array that is displayed in a table view.

tableView.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in

  self.objectsArray.exchangeObjectAtIndex(toIndexPath.row, withObjectAtIndex: fromIndexPath.row)

}

This works fine with a basic array, but I actually want to rearrange a managed object of a type NSSet. So in my file I have declared the following which creates returns an array of type Item which is used by the table view.

Folder class function:

 func itemArray() -> [Item] {
    let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
    return iist.sortedArrayUsingDescriptors([sortDescriptor]) as! [Item]
}

Table View Controller declaration

var itemArray = [Item]()
    itemArray = folder.itemArray() //class function to return a NSArray of the NSSET [Item]

I am trying to make it so when rearranging the cells it changes the order of the NSSet so it is saved when the app reloads, does anyone have any suggestions ?

By definition, NSSets do not have an order so there is no native way for you to preserve the order of the NSSet. I am not saying it is not possible but you cannot do it the way you are thinking.

From Apple's NSSet Documentation :

The NSSet, NSMutableSet, and NSCountedSet classes declare the programmatic interface to an unordered collection of objects.

You will need to convert your NSSet to NSMutableArray and reference that array instead of the NSSet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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