2013-02-13 49 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; 
}