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

一切正常,但如果我按下开始编辑时出现的取消按钮,我的列表不会回来,但搜索结果仍然存在。要显示列表,我必须开始输入,甚至是搜索栏中的单个字符,然后将其删除或按“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 方法中,将所有对象添加到过滤表数据中,并仅使用此数组重新加载表。

因此,您的方法看起来像-

 -(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 回答