在Custom UIViewController中实现固定的SearchBar和TableView

时间:2014-03-03 06:09:16

标签: ios uitableview ios7 uisearchbar

目的是实现一个固定的搜索栏,就像iOS7中的联系人一样。

我有一个名为SearchViewController的视图控制器,它继承自UIViewController。

我添加了一个searchBar和一个tableView作为其navigationController.view的子视图。

但是由于searchBar和tableView是分开的,当我开始搜索时,没有正确显示对tableView和结果表视图的暗淡效果。

我只是希望它的行为与联系人应用程序一样。

这是我的代码:

SearchViewController.h

#import <UIKit/UIKit.h>
@class UWTabBarController;
@class InfoSessionModel;

@interface SearchViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>

SearchViewController.m

#import "SearchViewController.h"
@interface SearchViewController ()

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *data;

@end

@implementation SearchViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // initiate search bar
    NSInteger statusBarHeight = 20;
    NSInteger navigationBarHeight = self.navigationController.navigationBar.frame.size.height;

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,  statusBarHeight + navigationBarHeight, 320, 44)];
//    _searchBar.tintColor = [UIColor clearColor];
    _searchBar.delegate = self;
    _searchBar.barStyle = UIBarStyleDefault;
    //NSMutableArray *scopeTitles = [[NSMutableArray alloc] initWithObjects:@"Employer", @"Program", @"Note", nil];
    _searchBar.scopeButtonTitles = [[NSArray alloc] initWithObjects:@"Employer", @"Program", @"Note", nil];//[@"Employer|Program|Note" componentsSeparatedByString:@"|"];


    // initiate search bar controller
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchController.delegate = self;
    _searchController.searchResultsDataSource = self;
    _searchController.searchResultsDelegate = self;

    // initiate table view
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, statusBarHeight + navigationBarHeight, 320, [UIScreen mainScreen].bounds.size.height - statusBarHeight - navigationBarHeight)];
    [_tableView setContentInset:UIEdgeInsetsMake(_searchBar.frame.size.height, 0, _tabBarController.tabBar.frame.size.height, 0)];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    [_tableView registerClass:[InfoSessionCell class]  forCellReuseIdentifier:@"InfoSessionCell"];
    [_tableView registerClass:[LoadingCell class] forCellReuseIdentifier:@"LoadingCell"];

    [self.navigationController.view addSubview:_tableView];
    [self.navigationController.view addSubview:_searchBar];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_searchController.searchResultsTableView == tableView) {
        return 1;
    }
    else {
        return [data count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_searchController.searchResultsTableView == tableView) {
        static NSString *cellIdentifier = @"LoadingCell";
        LoadingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

        cell.loadingLabel.text =  @"Test cell for search result";
        return cell;
    }
    else {
            //... configure cell and return cell
            return cell;
        }
    }
}

#pragma mark - UISearchDisplayController Delegate Methods
// hasn't been implemented

#pragma mark - UISearchBar Delegate Methods
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    //move the search bar up to the correct location
    [UIView animateWithDuration:.3
                     animations:^{
                         searchBar.frame = CGRectMake(searchBar.frame.origin.x,
                                                      20,// status bar's height
                                                      searchBar.frame.size.width,
                                                      searchBar.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         //whatever else you may need to do
                     }];
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    //move the search bar down to the correct location
    [UIView animateWithDuration:.25
                     animations:^{
                         NSInteger statusBarHeight = 20;
                         NSInteger navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
                         searchBar.frame = CGRectMake(_searchBar.frame.origin.x,
                                                      statusBarHeight + navigationBarHeight,
                                                      _searchBar.frame.size.width,
                                                      _searchBar.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         //whatever else you may need to do
                     }];
    return YES;
}

这是我的代码的效果: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

好吧,我的问题是由于我对视图和navigationController的观点的误解造成的。

在视图中加载方法: 之前是:

[self.navigationController.view addSubview:_tableView];
[self.navigationController.view addSubview:_searchBar];

但应该是:

[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];

这样,原始表视图和结果表视图将正确显示。

其他事情正在向上和向下移动搜索栏,这些事情应该通过UISearchBar委托协议方法和UISearchDisplayController委托协议方法完成。

这是实现固定searchBar和tableView的正确方法