4

私はこのコードを使用してUItableViewを検索しています:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {



if(searchText.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;


    if (filteredTableData == nil)
       filteredTableData = [[NSMutableArray alloc] init];
    else 
        [filteredTableData removeAllObjects];

    for (NSString* string in self.itemArray)
    {
        NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
        if(nameRange.location != NSNotFound)
        {
            [filteredTableData addObject:string];
        }
    }
}
[tableView reloadData]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

すべて正常に動作しますが、編集を開始したときに表示されるキャンセルボタンを押すと、リストが返されませんが、検索結果は残ります。リストを表示するには、検索バーに1文字でも入力を開始してから削除するか、「x」を押してすべてのリストを表示する必要があります。キャンセルボタンをブロックする方法はありますか?または、押されたときにリストを表示するには?

4

2 に答える 2

7

実装 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar UISearchBar のデリゲート メソッド。その中で、filteredTableData 配列にすべてのオブジェクト (テーブルの最初のオブジェクト) を追加し、テーブルをリロードします。

   -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {       
        [filteredTableData removeAllObjects];
        [filteredTableData addObjectsFromArray:self.itemArray];
        [tableView reloadData];
    }

また、テーブルをリロードするためのデータ ソース配列を選択するために使用している場合 (テーブル ビューのデータ ソース メソッドで)、フラグ isFiltered を維持する必要はありません。例えば

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if(isFiltered) 
      return filteredTableData.count 
   else
      return self.itemArray.count
}

常にfilteredTableData配列にデータを保持している場合、これを行う必要はありません。あなたの方法は次のようになります-

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return filteredTableData.count 
}

最初に、コントローラーの init または viewDidLoad メソッドで、filteredTableData にすべてのオブジェクトを追加し、この配列のみを使用してテーブルをリロードします。

したがって、あなたの方法は次のようになります-

 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
 {
     if(searchText.length == 0)
     {
         [filteredTableData removeAllObjects];
         [filteredTableData addObjectsFromArray:self.itemArray];
     }
     else
     {
        [filteredTableData removeAllObjects];

        for (NSString* string in self.itemArray)
        {
            NSRange nameRange = [string rangeOfString:searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
            if(nameRange.location != NSNotFound)
            {
                [filteredTableData addObject:string];
            }
        }
     }
     [tableView reloadData]; 
    }
于 2013-02-13T19:17:44.960 に答える
2

私はその方法を使用しませんが、次の方法を使用すると完全に機能します

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchString];
    self.filteredCustomers = [[self.customers filteredArrayUsingPredicate:predicate] mutableCopy];

    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchDisplayController setActive:NO animated:NO];
    self.filteredCustomers = nil;
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomerCell";
    NSDictionary *customerObject;

    CustomerListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (self.searchDisplayController.active) {
        customerObject = [[NSDictionary alloc] initWithDictionary:[self.filteredCustomers objectAtIndex:indexPath.row]];
    } else {
        customerObject = [[NSDictionary alloc] initWithDictionary:[[self.customerData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    }
    cell.textLabel.text = [customerObject objectForKey:@"name"];
    cell.customerName = [customerObject objectForKey:@"name"];
    cell.customerId = [customerObject objectForKey:@"customer_id"];

    return cell;
}
于 2013-02-13T19:17:06.587 に答える