4

私はiosを始めたばかりで、UITableView.

ビューの右側にインデックス バーを実装したいと考えています。これを実装しましたが、インデックス バーのセクションをタップしても機能しません。

これは私のコードです。indexArrayこれは「AZ」要素でfinalArrayあり、私の可変UITableView配列です。

で何を実装すればよいか教えてくださいsectionForSectionIndexTitle

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

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

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return indexArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:   (NSString *)title atIndex:(NSInteger)index
{

}
4

1 に答える 1

9

これはおそらく、 で最も紛らわしい方法の 1 つですUITableViewDataSource。例を検討するのが最善です。

4 つのアイテムを含むテーブル ビューがあるとします。

section index - 0 section title - 'A' 
items: ['Apple', 'Ant']

section index - 1 section title - 'C'
items: ['Car', 'Cat']

次に、26 のセクション インデックス タイトル (テーブル ビューの右側のインデックス) があります。A - Z

ユーザーが文字「C」をタップすると、電話がかかってきます

tableView:sectionForSectionIndexTitle:@"C" atIndex:2

'C' セクションのセクション インデックスである 1 を返す必要があります。

ユーザーが「Z」をタップした場合、セクションが 2 つしかなく、C が Z に最も近いため、1 を返す必要もあります。

右側のインデックスと同じセクションがテーブルビューにある単純なケースでは、これを実行しても問題ありません。

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

それ以外の場合は、セットアップによって異なります。セクションのタイトルでタイトルを探す必要がある場合があります。

 NSInteger section = [_sectionTitles indexOfObject:title];
 if (section == NSNotFound) // Handle missing section (e.g. return the nearest section to it?)
于 2013-07-09T12:58:30.230 に答える