2015-12-20 45 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