如何使用NSFetchedResultsController重新排序节之间的单元格

时间:2014-12-21 22:02:58

标签: tableview

我有一个表视图,允许用户在节之间重新排序单元格。

我有这个,它在一个包含一个部分的表格中工作,但是我无法弄清楚如何在部分之间改变它...

-(void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
      toIndexPath:(NSIndexPath *)destinationIndexPath;
{  
  NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];

  // Grab the item we're moving.
  NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

  // Remove the object we're moving from the array.
  [things removeObject:thing];
  // Now re-insert it at the destination.
  [things insertObject:thing atIndex:[destinationIndexPath row]];

  // All of the objects are now in their correct order. Update each
  // object's displayOrder field by iterating through the array.
  int i = 0;
  for (NSManagedObject *mo in things)
  {
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
  }

  [managedObjectContext save:nil];
}

请有人帮忙吗?

0 个答案:

没有答案