2013-02-13 81 views
4

我使用此代碼通過一個UITableView搜索:搜索表格視圖用的UISearchBar

-(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」查看所有列表。有沒有辦法阻止取消按鈕?或者在按下時顯示列表?

+0

您的代碼看起來是正確的。問題可能是你的'UITableViewDataSource'方法沒有正確處理過濾從'TRUE'變爲'FALSE'的情況(在obj-c中你應該使用'YES'和'NO')。另外,你不應該調用'reloadData'和'reloadSections:',你只需要調用一個。 – 2013-02-13 18:54:05

+0

如果'isFiltered'和'filteredTableData'是成員變量,那麼通常在它們前面加上一個下劃線'_' – nielsbot 2013-02-13 19:23:43

+0

'self.isFiltered = searchText.length> 0; self.tableData = self.isFiltered?問題是,如果我替換我的 - (void)searchBar:(UISearchBar * [self.itemArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@「SELF contains [cd]%@」,searchBar.text]]:self.itemArray;' – nielsbot 2013-02-13 19:26:24

回答

7

實現 - (無效)searchBarCancelButtonClicked:的UISearchBar的(*的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 
} 

最初在控制器的初始化或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]; 
    } 
+0

)searchBar textDidChange:(NSString *)searchText與你的,UISearchBar不搜索任何東西。我有我的最初名單... – user2014474 2013-02-13 19:21:16

+0

該方法 - (無效)searchBarCancelButtonClicked:(UISearchBar *)searchBar工作得很好,並回答我的問題,但我不明白你爲什麼說我應該刪除isFiltered – user2014474 2013-02-13 19:24:36

+0

更新了你的答案查詢 – 2013-02-14 04:55:11

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