sectionIndexTitlesForTableView keys are off?

家住魔仙堡 提交于 2019-12-11 04:44:45

问题


I have implemented a Section Index for my tableview. It returns the correct letters for what is stored in coredata (A B C D F G H I K M N O P Q R S T U V W). However the indexes are off.

If I click on the letter M it takes me to letter I, etc.. The name of the index I am attempting to sort on is name

What is there for me to do to fix the index?

I am also implementing numberOfSectionsInTableView,numberOfRowsInSection, and titeForHeaderInSection. The section headers are displayed correctly.

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

Including the FRC just in case it is relevant:

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

回答1:


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

Try to implement in this way.




回答2:


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


来源:https://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!