2014-03-03 241 views
0

一个固定的搜索栏和TableView中的目的是为了实现一个固定的搜索栏,就像在iOS7联系人。实现自定义的UIViewController

我有一个名为SearchViewController视图控制器从UIViewController中继承的。

而且我添加了一个搜索栏和的tableView作为其navigationController.view的子视图。

但是,由于搜索栏和的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

回答

0

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

鉴于没有加载方法: 之前就是:

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

,但应该是:

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

这样一来,原来的表视图和结果表视图会正确显示。

还有其它的事情正在搜索栏上下,这些事情应该通过委托的UISearchBar协议的方法和UISearchDisplayController委托协议的方法来完成。

这是一个正确的方法来实现一个固定searchBar和tableView它下面