UISearchDisplayController在搜索后隐藏searchBar

时间:2014-07-15 04:33:06

标签: ios uitableview uisearchbar uisearchdisplaycontroller

我正在使用UISearchDisplayController从我的列表中搜索。但是当我搜索一个角色时,它会隐藏searchBar。这是我开始搜索之前的快照:

enter image description here

这里隐藏searchBar,如下所示:

enter image description here

#pragma mark - UISearchDisplayController Delegate Methods
//These methods will be used for search controller frame
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

}

- (void) keyboardWillHide {

    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}

- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        for (UIView* v in self.searchDisplayController.searchResultsTableView.subviews) {
            if ([v isKindOfClass: [UILabel class]] &&
                [[(UILabel*)v text] isEqualToString:@"No Results"]) {
                [(UILabel *)v setText:NO_RESULT_LABEL_STRING];
                // .. do whatever you like to the UILabel here ..
                break;
            }
        }
    });

    [self handleSearchForTerm:searchString];
    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [[self dealsTableView] reloadData];
}

#pragma mark Search Method

- (void) handleSearchForTerm:(NSString *) searchString {

    //First get all the objects which fulfill the criteria

    [[self searchResultsArray] removeAllObjects];
    for (Deal *currentDeal in self.dealsArray)
    {
        NSArray *searchQueryComponents = [searchString componentsSeparatedByString:@" "];

        BOOL isGoAheadToAdd=YES;

        for (NSString *currentSearchComponent in searchQueryComponents) {

            NSString *trimmed = [currentSearchComponent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

            if ([trimmed length]>0) {
                if ([[currentDeal title] rangeOfString:currentSearchComponent options:NSCaseInsensitiveSearch].location != NSNotFound)
                {
                    isGoAheadToAdd=YES;
                }
                else {
                    isGoAheadToAdd=NO;
                    break;
                }
            }
        }

        if (isGoAheadToAdd) {
            [[self searchResultsArray] addObject:currentDeal];

        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

@interface MySearchDisplayController : UISearchDisplayController

@end

@implementation MySearchDisplayController

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    [super setActive: visible animated: animated];

    [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
}

答案 1 :(得分:0)

我必须为searchResultsTableView设置框架,因为它的原点是(0,0)。

由此解决:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    [tableView setFrame:CGRectMake(0, 42, 320, 380)];
}

默认情况下,UISearchDisplayControllersearchResultsTableView的框架设置为(0,0),因此searchBar隐藏在searchResultsTableView后面。