0

アプリでCore Dataを使用しています。私はUITableViewを持っていて、NSFetchedResultsControllerを使っています。CoreDataのUITableViewCellsとオブジェクトの並べ替え

これらは私の実体であると属性:

Entities

を私のセットアップは、次のようになります。

let fetchRequest = NSFetchRequest(entityName: "Timer") 
    let fetchSort = NSSortDescriptor(key: "orderBy", ascending: true) 
    let predicate = NSPredicate(format: "%K == %@", "timerWorkout", workout) 
    fetchRequest.predicate = predicate 
    fetchRequest.sortDescriptors = [fetchSort] 

    fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
    fetchedResultsController.delegate = self 

    do { 
     try fetchedResultsController.performFetch() 
    } 
    catch let error as NSError { 
     print("Unable to perform fetch: \(error.localizedDescription)") 
    } 

私はそれらのタイマーのリストを持ってトレーニングアイテムを持っています。ユーザーはタイマーを追加することができ、私は彼らが好きな方法で注文することができるようにしたいと思います。順序を指定できるように、orderBy属性を追加しました。新しい商品が追加されると、私はorderByの値を決定するためにこのコードを使用します:

if let timer = vc?.timer { 
     timer.orderBy = workout.workoutTimers?.count 
     workout.mutableSetValueForKey("workoutTimers").addObject(timer) 

     do { 
      try context.save() 
     } 
     catch { 
      print("Unable to add timer.") 
     } 
    } 

それはそれが機能します。私が実行している問題は、ユーザーがリスト内で項目を移動できるようにして項目を並べ替えることです。 UITableViewCellの項目を移動するのは簡単ですが、データセット内で項目を並べ替える方法を見つけることができませんでした。これは私が試みたことのいくつかです。コメントアウトされた行は、私が試したさまざまなものです。

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { 

    //let orderedSet = workout.valueForKey("workoutTimers") as? NSMutableOrderedSet 
    //myObject.valueForKeyPath("subObjects") as NSMutableSet 
    //let orderedSet = fetchedResultsController.mutableCopy() as! NSMutableOrderedSet 
    //let orderedSet: NSMutableOrderedSet = (fetchedResultsController?.mutableOrderedSetValueForKey("Timer"))! 
    //let orderedSet: NSMutableOrderedSet = (workout.mutableOrderedSetValueForKey("workoutTimers")) 
    //let orderedSet = workout.mutableOrderedSetValueForKey("workoutTimers") 
    let orderedSet = workout.workoutTimers?.mutableOrderedSetValueForKey("orderBy") 
    orderedSet.exchangeObjectAtIndex(sourceIndexPath.row, withObjectAtIndex: destinationIndexPath.row) 

    do { 
     try context.save() 
    } 
    catch { 
     print("Unable to reorder.") 
    } 
} 

これまでのところ、何も機能しません。私が大部分を得ている例外は、 です。このクラスは、キーのコードに準拠したキー値ではありません。

誰も私がこれで逃しているものについていくつかの支援を提供することはできますか?

編集:

質問のコメントを読んだ後、これは私が思い付いたものです。それはうまくいくと思われ、はるかにクリーンです。

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { 
    if let items: NSMutableArray = NSMutableArray(array: fetchedResultsController.fetchedObjects!) { 
     if let sourceItem = items.objectAtIndex(sourceIndexPath.row) as? Timer { 
      items.removeObject(sourceItem) 
      items.insertObject(sourceItem, atIndex: destinationIndexPath.row) 

      var index = 0 

      for item in items { 
       (item as? Timer)?.orderBy = index 
       index += 1 
      } 

      do { 
       try context.save() 
      } 
      catch { 
       print("Unable to reorder.") 
      } 

      timersTable.reloadData() 
     } 
    } 
} 
+0

あなたの関係は注文されていますか?順序関係を順序値属性と実際に混在させることはできません。 – Wain

答えて

0

実際には、順序付けされた関係で何もしないでください。項目の移動の索引にループを作成し、値を追加および減算して値を再編成する必要があります。移動されたアイテムには、移動のサイズが加算または減算され、それ以外のアイテムはすべて加算または減算されます。

+0

私はあなたが何を得ているのか理解していると思います。私は正しく動作するように思われる解決策で質問を更新しました。 –

関連する問題