【问题标题】:Implement a fixed SearchBar and TableView in a Custom UIViewController在自定义 UIViewController 中实现固定的 SearchBar 和 TableView
【发布时间】:2014-04-04 02:11:04
【问题描述】:

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

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

我添加了一个 searchBar 和一个 tableView 作为它的 navigationController.view 的 subView。

但是由于 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;
}

这是我的代码的效果:

【问题讨论】:

    标签: ios uitableview ios7 uisearchbar


    【解决方案1】:

    好吧,我的问题是我对views和navigationController的views的理解有误。

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

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

    但应该是:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多