sectionIndexTitlesForTableView键是关闭的?

时间:2011-11-07 14:08:37

标签: iphone uitableview indexing nsfetchedresultscontroller

我已经为我的tableview实现了一个Section Index。它返回存储在coredata(A B C D F G H I K M N O P Q R S T U V W)中的正确字母。然而,指数已经关闭。

如果我点击字母M,我就会给我写信等等。我试图排序的索引的名称是name

我有什么办法来修复索引?

我还在实施numberOfSectionsInTableViewnumberOfRowsInSectiontiteForHeaderInSection。部分标题正确显示。

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    NSArray * sectionTitlesArray = [fetchedResultsController sectionIndexTitles];
    NSMutableArray *newTitles = [[NSMutableArray alloc] init];
    for (NSString *state in sectionTitlesArray) {
        [newTitles addObject:[NSString stringWithFormat:@"%@", state]];
    }
    return [newTitles autorelease];
}

包括FRC以防万一它是相关的:

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"WidgetCoItems" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];

        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:nil];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor1 release];
        [sortDescriptor2 release];
        [sortDescriptors release];
    }   
    return fetchedResultsController;
}  

2 个答案:

答案 0 :(得分:2)

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
        NSMutableArray *indices = [NSMutableArray array];

        id <NSFetchedResultsSectionInfo> sectionInfo;

        for( sectionInfo in [fetchedResultsController sections] )
        {
            [indices addObject:[sectionInfo name]];
        }
        return indices;
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    return [[[fetchedResultsController sections] objectAtIndex:section] name];
}

尝试以这种方式实施。

答案 1 :(得分:0)

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
if ([[<#Fetched results controller#> sections] count] > 0) {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo name];
} else
    return nil;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [<#Fetched results controller#> sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [<#Fetched results controller#> sectionForSectionIndexTitle:title atIndex:index];
}