2012-07-28 52 views
6

我正在实施一个UISearchDisplayController,我想在输入文本之前使用正确加载的tableView(未过滤)内容填充searchResultsTableView如何在搜索栏被激活时显示searchResultsTableView?

这在我开始在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]; 
} 

有没有人有任何指向正确的方向?

请不要回答“不要那么做”或“苹果公司没有设计控制方式”。或...

+0

您是否找到解决方案?我也有完全一样的问题。 – nonamelive 2012-09-08 06:10:09

+0

有没有人制定过如何做到这一点呢? – Chicken 2014-06-05 02:06:50

回答

0

苹果公司的控制像那样工作。我通过实现一个单独的控制器(称为搜索显示控制器)对象来解决这个问题,该对象是一个搜索栏委托并将谓词应用于数据源。通过这种方式,tableview被“inline”过滤。

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

希望它能帮助你。

+0

嗨Midhun,感谢您的评论,但它并没有那么奏效。当我激活搜索时,searchDisplayController调暗tableView(虽然我们仍然看到内容但无法控制它)。当在searchBar中输入文本时,searchDisplayTableView会加载(过滤或不过滤,我可以控制)内容,我想显示。 – Ron 2012-07-28 15:05:10

+0

@Ronny:当你开始输入表格视图变暗时,在输入单个字母后,表格视图变为先前的状态(不变灰)。我认为这是你的问题,我是否正确? – 2012-08-01 15:34:32

+0

@Ronny:你还有问题吗? – 2012-08-02 11:48:53

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

我希望能为您服务,我的英语是非常有限的

相关问题