`
jsntghf
  • 浏览: 2478899 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

UISearchDisplayController的使用

    博客分类:
  • iOS
阅读更多

(1)SOLAddLocationViewController.h

#import <UIKit/UIKit.h>

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

@end

 

(2)SOLAddLocationViewController.m

#import "SOLAddLocationViewController.h"

#pragma mark - SOLAddLocationViewController Class Extension

@interface SOLAddLocationViewController ()

// Results of a search
@property (strong, nonatomic) NSMutableArray *searchResults;

@property (strong, nonatomic) NSMutableArray *allCitys;
@property (strong, nonatomic) NSArray *dataList;

// Location search results display controller
@property (strong, nonatomic) UISearchDisplayController *searchController;

// --------
// Subviews
// --------

// Location search bar
@property (strong, nonatomic) UISearchBar *searchBar;

// Navigation bar at the top of the view
@property (strong, nonatomic) UINavigationBar *navigationBar;

// Done button inside navigation bar
@property (strong, nonatomic) UIBarButtonItem *doneButton;

@property (strong, nonatomic) UITableView *mTableView;

@end

#pragma mark - SOLAddLocationViewController Implementation

@implementation SOLAddLocationViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor clearColor];
    self.view.opaque = NO;
    
    self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 480) style:UITableViewStyleGrouped];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    [self.mTableView setContentSize:CGSizeMake(320, 3000)];
    [self.view addSubview:self.mTableView];
    
    self.searchResults = [[NSMutableArray alloc]initWithCapacity:5];
    
    self.navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
    [self.view addSubview:self.navigationBar];
    
    self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                   target:self
                                                                   action:@selector(doneButtonPressed)];
    // Inititalize and configure search bar
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 44)];
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.searchBar.placeholder = @"搜索城市";
    self.searchBar.delegate = self;
    
    // Initialize and configure search controller
    self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.displaysSearchBarInNavigationBar = YES;
    self.searchController.navigationItem.rightBarButtonItems = @[self.doneButton];
    self.navigationBar.items = @[self.searchController.navigationItem];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Provinces" ofType:@"plist"];
    self.dataList = [NSMutableArray arrayWithContentsOfFile:path];
    
    self.allCitys = [NSMutableArray arrayWithCapacity:0];
}

#pragma mark UIViewController Methods

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [self.searchController setActive:YES animated:NO];
    [self.searchController.searchBar becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    [self.searchController setActive:NO animated:NO];
    [self.searchController.searchBar resignFirstResponder];
}

#pragma mark DoneButton Methods

- (void)doneButtonPressed {
    
}

#pragma mark UISearchDisplayControllerDelegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar                                                     selectedScopeButtonIndex]]];
    
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:searchOption]];
    
    return YES;
}

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    [tableView setFrame:CGRectMake(0, CGRectGetHeight(self.navigationBar.bounds), CGRectGetWidth(self.view.bounds),
                                   CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.navigationBar.bounds))];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    [self.view bringSubviewToFront:self.navigationBar];
}

- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
    NSMutableArray *searchResult = [[NSMutableArray alloc] initWithCapacity:0];
    
    for(int i = 0; i < [self.allCitys count]; i++) {
        NSString *str = [self.allCitys objectAtIndex:i];
        if([str rangeOfString:searchText].location != NSNotFound)
            [searchResult addObject:str];
    }
    
    self.searchResults = [NSArray arrayWithArray:searchResult];
}

#pragma mark UITableViewDelegate Methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CellIdentifier";
    
    // Dequeue cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // Configure cell for the search results table view
    if(tableView == self.searchController.searchResultsTableView)
        cell.textLabel.text = self.searchResults[indexPath.row];
    else
        cell.textLabel.text = self.dataList[indexPath.section][@"Citys"][indexPath.row][@"C_Name"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.searchController.searchResultsTableView) {
        [tableView cellForRowAtIndexPath:indexPath].selected = NO;
        // code
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(tableView == self.searchController.searchResultsTableView)
        return 1;
    else
        return [self.dataList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.searchController.searchResultsTableView)
        return [self.searchResults count];
    else {
        NSArray *cityArray = self.dataList[section][@"Citys"];
        
        for(int i = 0; i < [cityArray count]; i++) {
            NSString *Name = cityArray[i][@"C_Name"];
            if (![self.allCitys containsObject:Name])
                [self.allCitys addObject:Name];
        }
        
        return [cityArray count];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *HeaderName;
    
    if(tableView == self.searchController.searchResultsTableView)
        HeaderName = @"搜索结果";
    else
        HeaderName = self.dataList[section][@"p_Name"];
    
    return HeaderName;
}

@end

 

(3)效果图


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics