使用两个单独的NSFetchedResultsController's
然后,你需要在每个不同的委托方法考虑到这一点。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.mainFetchedResultsController sections] count] + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [[[self.favFetchedResultsController sections] objectAtIndex:section] numberOfObjects];
} else {
return [[[self.mainFetchedResultsController sections] objectAtIndex:section - 1] numberOfObjects];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"Favourites";
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.mainFetchedResultsController sections] objectAtIndex:section - 1];
return [sectionInfo name];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Object *object = nil;
if (indexPath.section == 0) {
object = [self.favFetchedResultsController objectAtIndexPath:indexPath];
} else {
NSIndexPath *mainIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section -1];
object = [self.mainFetchedResultsController objectAtIndexPath:mainIndexPath];
}
UITableViewCell *cell = ...
...
return cell;
}
你最终做了什么?我想下面的捕手是对的。 – Johan
我最终使用'NSFetchRequest'和'NSPredicate',因为浪费时间来计算这一点远远大于使用上述类的实例的简单实现。 – Eimantas