0

検索機能付きのテーブルビューを実装しようとしています。各アイテムの最初の文字を見る機能があります。次に、一意の文字ごとに、別の配列内に追加します。そのため、 'S' で始まるアイテムが内部にある場合、文字 'S' を 1 回だけ追加します。

次に、この配列を使用して、セクションと rowsForSections を構築します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows = 0;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        //---get the letter in each section; e.g., A, B, C, etc.---
        NSString *alphabet = [firstIndex objectAtIndex:section];
        //---get all states beginning with the letter---
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
        NSArray *names = [self.filteredListContent filteredArrayUsingPredicate:predicate];
        //---return the number of states beginning with the letter---
        rows =  [names count];
    }
    else
    {
        //---get the letter in each section; e.g., A, B, C, etc.---
        NSString *alphabet = [firstIndex objectAtIndex:section];
        //---get all states beginning with the letter---
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
        NSArray *names = [self.listContent filteredArrayUsingPredicate:predicate];
        //---return the number of states beginning with the letter---
        rows =  [names count];
    }
    return rows;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    int rows = 0;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        rows = [filteredFirstIndex count];
    }
    else
    {
        rows =  [firstIndex count];
    }
    return rows;
}

これが私のCellForRowAtIndexです

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"list content %@",[listContent valueForKey:@"name"]);
    static NSString *kCellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    /*
     If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
     */
    Contact *contact = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        contact = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        contact = [self.listContent objectAtIndex:indexPath.row];
    }

    cell.textLabel.text = contact.name;
    return cell;
}

問題 ここに私の問題のスクリーンショットがあります。写真は1000以上の言葉を語るからです。

ここに画像の説明を入力

誰でも私を助けてくれることを願っています!

敬具!

4

1 に答える 1

0

これらの行は問題を引き起こします:

    contact = [self.filteredListContent objectAtIndex:indexPath.row];

    contact = [self.listContent objectAtIndex:indexPath.row];

名前をセクションに表示しているためです。

各セクションで、indexPath.row値は 0 から始まります。

だからこそ、この結果を得ているのです。

以下は良い解決策ではありません。しかし、例として私はそれを投稿しています。(パフォーマンスの問題が発生します)あなたの中でそれを使用してくださいcellForRowAtIndexPath

NSString *alphabet = [firstIndex objectAtIndex:indexPath.section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
NSArray *names = [self.filteredListContent filteredArrayUsingPredicate:predicate];
contact = [names objectAtIndex:indexPath.row];
于 2012-12-26T10:39:58.093 に答える