ключи sectionIndexTitlesForTableView отключены? - PullRequest
1 голос
/ 07 ноября 2011

Я реализовал индекс раздела для моего просмотра таблицы. Он возвращает правильные буквы для того, что хранится в coredata (A B C D F G H I K M N O P Q R S T U V W). Однако индексы отключены.

Если я нажму на букву M, я перейду к букве I и т. Д. Имя индекса, по которому я пытаюсь отсортировать, - name

Что мне делать, чтобы исправить индекс?

Я также реализую numberOfSectionsInTableView, numberOfRowsInSection и titeForHeaderInSection. Заголовки разделов отображаются правильно.

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

2 голосов
/ 07 ноября 2011
- (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];
}

Попробуйте реализовать таким способом.

0 голосов
/ 01 декабря 2013
- (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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...