`
dcj3sjt126com
  • 浏览: 1826804 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

UISearchBar和UISearchBarController的使用

    博客分类:
  • IOS
阅读更多
//
//  FirstViewController.m
//  UISearchBarControllerText
//
//  Created by 杜承玖 on 14/10/24.
//  Copyright (c) 2014年 com.redianying. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()
<UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UISearchBar * sb;
@property (nonatomic, strong) UITableView * tv;
@property (nonatomic, strong) UISearchDisplayController * sdc;
@property (nonatomic, copy) NSArray *allItems;
@property (nonatomic, copy) NSArray *searchResults;
@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSArray *items = @[@"Code Geass", @"Asura Cryin'", @"Voltes V", @"Mazinger Z", @"Daimos"];
    self.allItems = items;
    
    _sb = [[UISearchBar alloc] init];
    _sb.placeholder = @"输入名称";
    _sb.barStyle = UIBarStyleDefault;
    _sb.delegate = self;
    [_sb setScopeButtonTitles:@[@"First", @"Last"]];
    [_sb setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    
    _tv = [[UITableView alloc] initWithFrame:self.view.frame];
    _tv.frame = CGRectMake(0, 22, 320, 500);
    _tv.delegate = self;
    _tv.dataSource = self;
    [self.view addSubview:_tv];
    [_tv setTableHeaderView:_sb];
    
    _sdc = [[UISearchDisplayController alloc] initWithSearchBar:_sb contentsController:self];
    _sdc.delegate = self;
    [_sdc setSearchResultsDataSource:self];
    [_sdc setSearchResultsDelegate:self];
}

#pragma mark - SearchBar
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
//    _tv.frame = CGRectMake(0, 22, 320, 500);
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
//    _tv.frame = CGRectMake(0, 22, 320, 500);
    return YES;
}

#pragma mark - SearchDisplayController
- (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;
}

#pragma mark - Function
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",                                     searchText];
    self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}


#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rows = 0;
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
        rows = [self.searchResults count];
    }else{
        rows = [self.allItems count];
    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    /* Configure the cell. */
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    }else{
        cell.textLabel.text = [self.allItems objectAtIndex:indexPath.row];
    }
    return cell;
    
}


@end

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics