索引NSFetchedResultsController

时间:2012-01-19 14:58:48

标签: ios uitableview core-data nsfetchedresultscontroller

如何索引NSFetchedResultsController以便我可以在tableview上实现A-Z索引。

我在初始化程序中看到我可以sectionNameKeyPath,但这只是在自己的部分中放置了唯一的对象。

以下是我NSFetchedResultsController

的内容
    - (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"name" cacheName:@"Customer"];
    aFetchedResultsController.delegate = self;

    self.fetchedResultsController = aFetchedResultsController;

    return __fetchedResultsController;
}  

4 个答案:

答案 0 :(得分:13)

像这样实施sectionIndexTitlesForTableView:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.fetchedResultsController sectionIndexTitles];
}

这将为tableView的右侧提供那些索引。

如果您希望您的部分具有A,B,C,D等名称,则必须实现一个返回对象首字母的方法。

类似的东西:

- (NSString *)firstLetter {
    [self willAccessValueForKey:@"firstLetter"];
    NSString *firstLetter = [[[self name] substringToIndex:1] uppercaseString];
    [self didAccessValueForKey:@"firstLetter"];
    return firstLetter;
}

这将进入您的coredata实体的自定义子类。

然后将名为firstLetter的瞬态属性添加到核心数据实体,并将NSFetchedResultsController init中的sectionNameKeyPath替换为firstLetter

答案 1 :(得分:1)

@Harris的回答将在表格的一侧显示整个字母。不幸的是,如果你点击其中一个 实际上有相应部分的字母,你就会崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Index title at 3 is not equal to 'K''

答案 2 :(得分:0)

如果你想在侧面加上字母A-Z(即使你没有每个字母的部分),这样做:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
  index = 0;
  for(id sectionInfo in [_fetchedResultsController sections]){
    if ([[[sectionInfo name] uppercaseString] isEqualToString:title]){
      break;
    }
    index++;
  }
  if (index>=[_fetchedResultsController sections].count){
    return -1;
  }

  return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

答案 3 :(得分:0)

正确版本(ios 7.1至8.3):

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger section = 0;
    for (id <NSFetchedResultsSectionInfo> sectionInfo in [_fetchedResultsController sections])
    {
        if ([sectionInfo.indexTitle compare:title] >= 0)
        {
            break;
        }
        section++;
    }
    return section;
}