如何显示搜索结果表格视图何时激活搜索栏?

时间:2012-07-28 05:43:54

标签: ios uisearchdisplaycontroller

我正在实现UISearchDisplayController,我希望在输入文本之前,在加载时将searchResultsTableView填充tableView(未过滤)的内容。

当我开始在searchBar中输入值时,这是有效的。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.isSearching = YES;

    //tell the searchDisplayTableView to show here!

    [controller.searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.isSearching = NO;
    [controller.searchBar setShowsCancelButton:NO animated:YES];
}

有人会指出正确的方向吗?

请不要回复“不要那样做”或“Apple没有设计控制方式”。或者......

4 个答案:

答案 0 :(得分:0)

在搜索过程中,您需要使用过滤后的结果重新加载表格视图: 在此方法中,只需调用表视图reloadData

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.isSearching = YES;
    [yourTableView reloadData];
    [controller.searchBar setShowsCancelButton:YES animated:YES];
}

并修改您的numberOfSectionsInTableView方法,如:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(isSearching == YES)
   {
        yourDataArray = //filtered result;
   }
   else
   {
        yourDataAray = //unfiltered result;
   }
   return 1;
}

希望它能帮到你。

答案 1 :(得分:0)

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    // Here is a table we want to show in UISearchDisplayController
    XXRecentSearchDataSource *recentSearch = [XXRecentSearchDataSource recentSearchDataSource];
    recentSearch.delegate = self;

    // Setup default table view
    CGRect frame = CGRectMake(0.0f,
                       CGRectGetMaxY(controller.searchBar.frame),
                       CGRectGetWidth(self.view.bounds),
                       CGRectGetHeight(self.view.bounds) - CGRectGetHeight(controller.searchBar.bounds));

    // Setup temporary table and remember it for future use
    [_tempRecentSearchTableView release];
    _tempRecentSearchTableView = [[UITableView alloc] initWithFrame:frame
                                                               style:UITableViewStylePlain];
    _tempRecentSearchTableView.dataSource = recentSearch;
    _tempRecentSearchTableView.delegate = recentSearch;

    [self performSelector:@selector(removeOverlay) withObject:nil afterDelay:.0f];
}

- (void)removeOverlay
{
    [[self.view.subviews lastObject] removeFromSuperview];
    [self.view addSubview:_tempRecentSearchTableView];
}

在某些情况下,您必须记住删除_tempRecentSearchTableView。例如:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // Remove temporary table view
    [_tempRecentSearchTableView removeFromSuperview];
}

答案 2 :(得分:0)

你知道搜索栏是否有焦点。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_searchBar isFirstResponder]) {
              return [_filteredData count];
    } else {

        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];

    }
}

配置单元格时,请执行相同的问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"CustomCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Entidad *e = nil;

    if ([_searchBar isFirstResponder])
    {
        e = [self.filteredData objectAtIndex:indexPath.row];

    }
    else
    {
        e = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }

    cell.nameLabel.text = e.titulo;
    cell.thumbnailImageView.image = [UIImage imageNamed:@"password-50.png"];
    cell.dateLabel.text = e.sitio;

    return cell;
}

设置过滤器参数

-(void)filter:(NSString*)text
{

    _filteredData = [[NSMutableArray alloc] init];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];


    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"titulo" ascending:YES];
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];


    if(text.length > 0)
    {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text];
        [fetchRequest setPredicate:predicate];
    }

    NSError *error;


    NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    _filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities];

    NSLog(@"%@", _filteredData);

    [self.tableView reloadData];
}

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    [self filter:text];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

    [self filter:@""];

    [_searchBar resignFirstResponder];
    [_searchBar setText:@""];
    [_tableView reloadData];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

    [_searchBar setShowsCancelButton:YES animated:YES];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [_searchBar setShowsCancelButton:NO animated:YES];
}

我希望为你服务,我的英语非常有限

答案 3 :(得分:0)

Apple的控制就是这样的。我通过实现一个单独的控制器(称为搜索显示控制器)对象来解决它,该对象是一个搜索栏委托并将谓词应用于数据源。这样,tableview被“内联”过滤。