2012-01-19 29 views
4

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

我在初始化程序中看到我可以做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; 
} 

回答

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到你的核心数据实体的瞬时属性,如果你想有一侧的字母AZ(即使你没有部分与firstLetter

+0

这个工作完美!吹我的脑海。现在,它只在我的数据库中的对象索引中显示字母。所以如果我在那里只有“测试,测试,测试”,它只显示T,我该如何显示所有字母以及如何处理带有“#”的数字?我是否可以放置与其他解决方案类似的字母数组? – tofortier

+1

在我的firstLetter方法中以这种方式找出数字事物。 if([[[[NSNumberFormatter alloc] init] numberFromString:firstLetter]){ return @“#”; } – tofortier

+0

吹我的脑海!真棒感谢 –

0

更换sectionNameKeyPath在NSFetchedResultsController INIT对于每一个字母),这将做到这一点:

- (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]; 
} 
1

@Harris的回答将显示在表的一侧全字母。不幸的是,如果你再轻按实际上有一个相应部分的一个字母,你喜欢的东西崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Index title at 3 is not equal to 'K'' 
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; 
} 
相关问题