带有索引的UITableViewController和UILocalizedIndexedCollat​​ion的NSFetchedResultsController

时间:2013-02-16 00:29:54

标签: objective-c nsfetchedresultscontroller uilocalizedcollation

我有一个使用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];
}

但我想使用UILocalizedIndexCollat​​ion来获得完整的字母表。

如何将这些方法连接到NSFetchedResultsController?

我想我需要从当前的Collat​​ion

获取索引标题
- (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 个答案:

答案 0 :(得分:6)

我认为你必须遍历获取的结果控制器部分并找到给定标题的匹配部分,例如:

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

对于没有匹配部分的部分索引标题,您必须决定是否要跳转到“较低”或“较高”部分。上面的方法跳转到下一个更高的部分。

答案 1 :(得分:1)

我从马丁那里得到的解决方案。

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