2

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 に接続するにはどうすればよいですか?

現在の照合順序からインデックス タイトルを取得する必要があると思います

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

2 に答える 2

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

一致するセクションを持たないセクション インデックス タイトルについては、「下位」または「上位」のセクションにジャンプするかどうかを決定する必要があります。上記のメソッドは、次の上位セクションにジャンプします。

于 2013-02-16T15:10:28.390 に答える
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;
}
于 2013-02-18T03:26:25.403 に答える