1

我想创建一个 UISearchBar,可以从我的应用程序的多个视图中查看和使用。

出于这个原因,我在我的 AppDelegate 类中声明了一个 UISearchBar 属性,并在我的第一个 UIViewController 中对其进行了初始化。

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

    IBOutlet UIWindow *window;

}

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

我的第一个视图.h

@interface MainTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>{

    AppDelegate *appDelegate;
    NSArray *searchResults;
}

@property (strong, nonatomic) IBOutlet UIBarButtonItem *accountPage;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *search;

我的第一个视图.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        self.search = [self.search initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearchBar:)];


    }

        - (IBAction)showSearchBar:(id)sender{

            UIBarButtonItem *searchItem = (UIBarButtonItem *) sender;

            if (searchItem.tag == 0) {
                if (!appDelegate.searchBar) {
                    appDelegate.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
                    [[UISearchDisplayController alloc] initWithSearchBar:appDelegate.searchBar contentsController:self];
                    self.searchDisplayController.searchResultsDelegate = self;
                    self.searchDisplayController.searchResultsDataSource = self;
                    self.searchDisplayController.delegate = self;
                    [self.view addSubview:appDelegate.searchBar];
                } else{
                    [appDelegate.searchBar setHidden:NO];
                }
                searchItem.tag = 1;
            } else{
                searchItem.tag = 0;
                [appDelegate.searchBar setHidden:YES];
            }
        }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } 
    // Set up the cell
    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSLog(@"filterContentForSearchText");
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"(name contains[cd] %@)",
                                    searchText];

    searchResults = [appDelegate.elements filteredArrayUsingPredicate:resultPredicate];
}

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    [self filterContentForSearchText:searchString
                               scope:[[appDelegate.searchBar scopeButtonTitles]
                                      objectAtIndex:[appDelegate.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

但是,虽然搜索栏在 VC 上初始化并显示正常,但搜索机制却无法正常工作。我可以在搜索文本框中写字,但没有任何动作。甚至键盘上的“搜索”按钮也不起作用。

4

2 回答 2

0

您必须UISearchBarDelegate根据您要使用的方式在相应的控制器类中实现方法。

参考这个

文档 UISearchBarDelegate

扩展答案:

在您的“我的第一个视图 .m”文件中,您需要实现 UISearchBarDelegate 方法才能获得您的操作。

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{   
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //This is where your search button action fires. You can implement what you want to do after search button click in here.   
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
   //This fires whenever you change text in your search bar. For an example if you want to show search suggestions when a new letter is typed you can implement that in here.
}

您可以根据需要使用其他委托方法。

于 2013-06-04T11:29:57.070 回答
-1

在 XIB 中使用搜索栏和搜索显示控制器对象。只需将此对象拖放到 XIB 中

并实现表视图委托和数据源方法。

喜欢

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView == self.aTableView)
    return [self.categories count];//normal table view
    return [self.searchCategories count];//search display controllers TableView
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"reusableCells";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:identifier] autorelease];
    NSString *text = nil;
    if(tableView == self.aTableView)
        text = [self.categories objectAtIndex:indexPath.row];
    else
       text = [self.searchCategories objectAtIndex:indexPath.row];

    cell.textLabel.text = text;

    return cell;
}

搜索显示控制器方法

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    if(self.searchCategories == nil){
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        self.searchCategories = arr;
        [arr release];
    }else{
        [self.searchCategories removeAllObjects];
    }

    for (NSString *aStr in self.categories) {
        if([[aStr lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound){
           [self.searchCategories addObject:aStr];
        }
    }
    [controller.searchResultsTableView reloadData];
    return NO;
}



- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    [self.aTableView setHidden:YES];
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
    [self.aTableView setHidden:NO];
}

希望这会帮助你。如果您仍有任何问题,我建议您阅读详细信息

UISearchDisplayController
于 2013-06-04T12:29:55.327 回答