Tasks
개체를 생각해 봅시다. 정렬시 사용할 필드를 추가해야합니다.
내부 Tasks.h
@interface Tasks : NSManagedObject
...
@property (nonatomic, retain) NSNumber * index; // also update your codedata model to add a numeric 'index' field to it (Integer 64 for instance)
@end
또한
구현에 합성 (@dynamic index;
); 이미 기존 Tasks
객체가있는 경우
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
NSManagedObjectContext *context = [slef ManagedObjectContext];
Tasks *tfrom = [tasks objectAtIndex:fromIndexPath.row];
Tasks *tto = [tasks objectAtIndex:toIndexPath.row];
tfrom.index = [NSNumber numberWithInteger:toIndexPath.row];
tto.index = [NSNumber numberWithInteger:fromIndexPath.row];
// preferably save the context, to make sure the new order will persist
[managedObjectContext save:nil]; // where managedObjectContext is your context
[tasks exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndexPath:toIndexPath.row]; //tasks is my array
[tableview reloadData];
}
것은, 당신이 그들에게 index
필드를 설정해야합니다 : 재정렬 핸들, 마지막으로
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tasks" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// set the sort descriptors to handle the sorting
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
self.tasks = [[[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy] autorelease];
[request release];
:
은 어디든지 당신은 작업을 인출 할 두 개의 타스크가 동일한 색인을 갖지 않도록하십시오.
'tasks'에는 무엇이 포함되어 있습니까? 'tasks'에서 관리 객체는'index' 필드를 포함합니까? –
작업은 MOC에서 가져온 배열 객체입니다. –
그래, 어떻게 배열에서 초기 순서를 설정합니까? 난 당신이 응용 프로그램을 열 때 이전 세션에서 (일부 행을 이동 한 후) 주문 '작업'을 기대합니다. 당신은이 순서를 유지할 필요가있다. 당신은'tasks' 객체에 그들의 순서를 알려주는 필드가 없어도 그것을 할 수 없다. 관리 객체 ('tasks'에서 사용)의 헤더와'tasks'를 채우기위한 가져 오기 요청을 제공하십시오. –