簡體   English   中英

使用UISearchBar搜索表視圖

[英]Searching a table view with UISearchBar

我正在使用此代碼搜索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];
}

一切正常,但如果我按下開始編輯時出現的取消按鈕,我的列表不會回來,但搜索結果仍然存在。 要顯示列表,我必須開始輸入,甚至是搜索欄中的單個字符,然后將其刪除或按“x”查看所有列表。 有辦法阻止取消按鈕嗎? 或者在按下時顯示列表?

實現 - (void)searchBarCancelButtonClicked:(UISearchBar *)UISearchBar的searchBar委托方法。 在那里添加所有對象(表中的初始對象)在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]; 
    }

我不使用該方法,但使用以下方法,它完美地工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM