【问题标题】:UINavigationController with UISearchBar and UISearchDisplayController带有 UISearchBar 和 UISearchDisplayController 的 UINavigationController
【发布时间】:2014-06-24 05:15:36
【问题描述】:

这里有几张照片开始:

这是我目前的主视图:

那么当用户选择 UISearchBar 时,会发生这种情况:

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

这是目前为止的代码:

- (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中的数据都是从代码中抽象出来的。

【问题讨论】:

  • 你想改变搜索栏的框架和大小还是颜色。或者总是显示取消按钮
  • 他想在执行搜索时禁用 backgroundview 视图的用户交互。

标签: ios objective-c uinavigationbar uisearchbar uisearchdisplaycontroller


【解决方案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.

在你看来DidLoad

- (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 的委托方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多