2012-05-20 42 views
2

的两个部分我有一个NSManagedObject其中有三个属性:显示管理对象在NSFetchedResultsController

  1. 头(NSString *
  2. 标题(NSString *
  3. 收藏夹(BOOL

我想使用以下方案显示这些对象的列表:

  • 收藏
    • 对象1
    • 对象3
  • 甲部首
    • 对象2
    • 对象3
  • 乙部首
    • 对象1
    • 对象4

有没有办法做到这一点使用NSFetchedResultsController?我试图用favorite,header对它进行排序,因为一旦对象被分配到收藏夹部分 - 它不会显示在其标题部分。有什么我可以使用的技巧吗?我应该执行两次提取并将结果重新格式化为一个嵌套数组?

+0

你最终做了什么?我想下面的捕手是对的。 – Johan

+1

我最终使用'NSFetchRequest'和'NSPredicate',因为浪费时间来计算这一点远远大于使用上述类的实例的简单实现。 – Eimantas

回答

1

使用两个单独的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; 
} 
相关问题