NSFetchedResultsController с индексированными UITableViewController и UILocalizedIndexedCollation

У меня есть таблица, которая использует NSFetchedResultsController. Это дает мне индекс с заголовками, которые присутствуют

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[_fetchedResultsController sections] count];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    NSArray *sections = _fetchedResultsController.sections;
    if(sections.count > 0)
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
        numberOfRows = [sectionInfo numberOfObjects];
    }
    return numberOfRows;
}

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

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

но я хочу использовать UILocalizedIndexCollation, чтобы получить полный алфавит.

Как я подключаю эти методы к NSFetchedResultsController?

Я думаю, что мне нужно получить заголовки индекса из текущего сопоставления

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}

но я заблудился о том, как написать этот метод

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //How do I translate index here to be the index of the _fetchedResultsController?
    return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}    

2 ответа

Решение

Я думаю, что вы должны пройти через извлеченные разделы контроллера результатов и найти соответствующий раздел для данного заголовка, например:

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

Для заголовков разделов, которые не имеют соответствующего раздела, вы должны решить, хотите ли вы перейти к "более низкому" или "более высокому" разделу. Вышеуказанный метод переходит к следующему более высокому разделу.

Решение, которое я выбрал, по вдохновению от Мартина.

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger localizedIndex = [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
    NSArray *localizedIndexTitles = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
    for(int currentLocalizedIndex = localizedIndex; currentLocalizedIndex > 0; currentLocalizedIndex--) {
        for(int frcIndex = 0; frcIndex < [[_fetchedResultsController sections] count]; frcIndex++) {
            id<NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:frcIndex];
            NSString *indexTitle = sectionInfo.indexTitle;
            if([indexTitle isEqualToString:[localizedIndexTitles objectAtIndex:currentLocalizedIndex]]) {
                return frcIndex;
            }
        }
    }
    return 0;
}
Другие вопросы по тегам