, 나는이 주문 속성을 사용하고 그에 따라이 업데이트하는 것이 좋습니다 많은 게시물을 가로 질러왔다, 하나 개의 코드는재주문 사용하여 가져 오기가 ResultsController
아래 여기에있다 새 개체를 삽입하는 동안 내가 여기 에 따라 표시 순서를 설정하고 증가해야하는 코드입니다 그것을
- (void)insertNewObject
{
Test *test = [NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject];
float lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] floatValue];
[test setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"];
}
- (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"];
}
[things release], things = nil;
// [managedObjectContext save:nil];
NSError *error = nil;
if (![managedObjectContext save:&error])
{
NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
NSLog(@"%@\n\nDetails: %@", msg, details);
}
// re-do the fetch so that the underlying cache of objects will be sorted
// correctly
if (![fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
하지만 100 개 항목이 있고, 그때 중간에서 어느 하나 개의 항목을 삭제 가정 내가 생각하는 displayOrder를 다시 계산해야한다. 이 과정을 (를) 대체 할 수있는 다른 방법이 있나요
감사 란짓
누군가 나를 도와 줄 수 있습니다. – Ranjit