UINavigationController与UISearchBar和UISearchDisplayController

时间:2014-06-24 05:15:37

标签: ios objective-c uinavigationbar uisearchbar uisearchdisplaycontroller

下载几张照片:

这是我目前对主视图的看法:

MainView

然后当用户选择UISearchBar时,会发生以下情况:

Current

而不是第二张照片,我希望视图更改为:

Clicked

以下是目前的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    /*
    self.searchView = [[UserSearchView alloc] initWithFrame:self.view.frame];
    self.searchView.searchResults.delegate = self;
    self.searchView.searchResults.dataSource = self;
    [self.view addSubview:self.searchView];
    */

    self.mainTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.mainTableView setDelegate:self];
    [self.mainTableView setDataSource:self];
    [self.mainTableView setShowsHorizontalScrollIndicator:NO];
    [self.mainTableView setShowsVerticalScrollIndicator:NO];
    [self.view addSubview:self.mainTableView];


    self.searchBar = [[UISearchBar alloc] init];
    [self.searchBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.searchBar setAutocorrectionType:UITextAutocorrectionTypeNo];
    [self.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

    self.searchCon = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    [self.searchCon setDelegate:self];
    [self.searchCon setSearchResultsDataSource:self];
    [self.searchCon setSearchResultsDelegate:self];

    self.navigationItem.titleView = self.searchCon.searchBar;

    /* This generates the result I want but I do not want the search
     * bar in the tableviews header, but rather in the uinavigationcontroller's
     * uinavigationbar. 
     */
    //self.mainTableView.tableHeaderView = self.searchBar;

    [self.view setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark -
#pragma mark - UISearchDisplayController Delegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if ([searchString isEqualToString:@""])
    return NO;

    PFQuery *query = [PFUser query];
    [query whereKey:@"username" containsString:searchString];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.array = [NSMutableArray arrayWithArray:objects];
            [self.searchDisplayController.searchResultsTableView reloadData];
            [self.mainTableView reloadData];
        } else {
            [[[UIAlertView alloc] initWithTitle:@"Failed Search" message:[NSString stringWithFormat:@"%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }
    }];
    return YES;
}


- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    [controller.searchBar setShowsCancelButton:YES animated:YES];
    NSLog(@"%@", controller);
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    [controller.searchBar setShowsCancelButton:NO animated:YES];
    NSLog(@"%@", controller);
}

#pragma mark - 
#pragma mark - UITableView Delegate & Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    [cell.textLabel setText:[[self.array objectAtIndex:[indexPath row]] objectForKey:@"username"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProfileViewController *profile = [[ProfileViewController alloc] init];
    [profile setProfileUser:[self.array objectAtIndex:[indexPath row]]];
    [self.navigationController pushViewController:profile animated:YES];
}

我对如何使目标视图工作感到很遗憾。查看Facebook应用程序是我想要重现的,它们的搜索栏的实现方式。

此外,我正在使用PARSE框架。因此,填充到tableview中的大量数据都是从代码中抽象出来的。

1 个答案:

答案 0 :(得分:1)

试试这个...... 在你的.h文件中

@interface YourtViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (nonatomic,strong)  UISearchDisplayController* searchDisplayController;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar; //connect this IBOutlet to your serach bar in your storyboard or xib.

在viewDidLoad中

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mySearchBar.delegate=self;
    self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.mySearchBar contentsController:self];
    self.searchDisplayController.delegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    self.searchDisplayController.searchResultsDelegate = self;

    // do your additional code here
    //

}

现在实现searchDisplayController的委托方法。