2015-12-20 41 views
1

阅读了超过10篇使用Obj-C编写的文章& Swift。 我仍然不知道如何使用CoreData重新排列列表中的行。在Swift 2中使用CoreData的moveRowAtIndexPath

因为我知道如何添加一个新的对象,以及如何删除一个我试图合并在一起都和我moveRowAtIndexPath方法成为

let prefToMove = mainVC.user.prefs[fromIndexPath.row] as! Pref 

var prefs = mainVC.user.prefs.mutableCopy() as! NSMutableOrderedSet 
prefs.addObject(prefToMove) 

mainVC.user.prefs = prefs.copy() as! NSOrderedSet 

// managed object context saving 
var error: NSError? 
do { 
    try managedContext!.save() 
} catch let error1 as NSError { 
    error = error1 
    print("Could not save: \(error)") 
} 
tableView.insertRowsAtIndexPaths([toIndexPath], withRowAnimation: .None) 

prefs = mainVC.user.prefs.mutableCopy() as! NSMutableOrderedSet 
prefs.removeObjectAtIndex(fromIndexPath.row) 
mainVC.user.prefs = prefs.copy() as! NSOrderedSet 

managedContext.deleteObject(prefToMove) 

// managed object context saving 
do { 
    try managedContext.save() 
} catch let error1 as NSError { 
    error = error1 
    print("Could not save: \(error)") 
} 

// Delete the row from the data source 
tableView.deleteRowsAtIndexPaths([fromIndexPath], withRowAnimation: .None) 

tableView.reloadData() 

但是,这是行不通的。删除部分工作但插入失败。 有人可以给我任何帮助吗?

+3

只是一句话:如果你使用英文名称作为变量,方法等,更多的读者会理解你的代码。 –

+0

@MartinR按要求修改。感谢您的建议 – Nicholas

回答

1

当你移动行时,你并不是真的删除或插入任何东西。无论您的数据集,尝试exchangeObjectAtIndex

override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

    let orderedSet: NSMutableOrderedSet = (routineToReorder?.mutableOrderedSetValueForKey("yourKeyValue"))! 

    orderedSet.exchangeObjectAtIndex(fromIndexPath.row, withObjectAtIndex: toIndexPath.row) 

    saveManagedObjectContext() // I have a standalone method for this that I call from several places 
} 

更新

这里的saveManagedObjectContext()样子:

func saveManagedObjectContext() { 
    if self.managedObjectContext.hasChanges { 
     do { 
      try managedObjectContext.save() 
     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      let nserror = error as NSError 
      NSLog("Unresolved error \(nserror), \(nserror.userInfo)") 
     } 
    } 
} 
+0

您的代码不太容易理解,也许仅针对专业人士。我不明白我应该把它作为'routineToReorder' – Nicholas

0

我想我想通了感谢@AdrianB因为我没有知道方法orderedSet.exchangeObjectAtIndex

在我的情况下,以下implement法在作品

let preferiti = calcoloVC.utenteCorrente.preferiti.mutableCopy() as! NSMutableOrderedSet 
preferiti.exchangeObjectAtIndex(fromIndexPath.row, withObjectAtIndex: toIndexPath.row) 
calcoloVC.utenteCorrente.preferiti = preferiti.copy() as! NSOrderedSet 

var error: NSError? 
do { 
    try managedContext.save() 
} catch let error1 as NSError { 
    error = error1 
    print("Could not save: \(error)") 
} 

现在我需要弄清楚在哪里写saveManagedObjectContext,而不是每次重写它。

+0

添加用于保存'managedObjectContext'的代码。查看我的答案更新。 – Adrian

+0

@AdrianB谢谢。你把它放在哪里? CoreDataStack.swift? – Nicholas

+1

卡在课堂上。 – Adrian