6

我正在实现一个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

4 回答 4

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

希望它会帮助你。

于 2012-07-28T09:32:44.093 回答
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];
}
于 2013-01-22T21:30:10.147 回答
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];
}

我希望能为您服务,我的英语很有限

于 2014-05-03T15:30:39.203 回答
0

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

于 2014-09-16T22:20:44.890 回答