我想要做的是每 x 行创建 NSFetchedResultsController 部分。
我尝试制作一个瞬态属性,它将根据其索引给出其部分,如下所示:
- (NSNumber*)section
{
[self willAccessValueForKey:@"section"];
NSManagedObjectContext* context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Animal"];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];
NSArray* array = [context executeFetchRequest:fetchRequest error:NULL];
int order = [array indexOfObject:self];
NSNumber *tmpValue = [NSNumber numberWithInt:order / 3];
[self didAccessValueForKey:@"section"];
return tmpValue;
}
并通过在每个对象上触发此功能使视图控制器刷新该部分:
-(void)refreshSection
{
NSManagedObjectContext* context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Animal"];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];
NSArray* array = [context executeFetchRequest:fetchRequest error:NULL];
int order = [array indexOfObject:self];
[self setSection:[NSNumber numberWithInt:order / 3]];
}
此解决方案有效,但对大量对象无效。
基本上,我想要实现的是:
第 0 节:
a
b
d
第 1 节:
e
如果我添加 c :
第 0 节:
a
b
c
第 1 节:
d
e
有没有其他更优雅的方式来做到这一点?
谢谢..