UISearchDisplayController UISearchBar

分组表+本地搜索 UISearchDisplayController  UISearchBar 的使用

效果图



@interface CityListViewController :UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

 

@property (nonatomic, retain) UITableView*mTableView;

 

@property (nonatomic, retain) NSArray*dataList;

 

@property (nonatomic, retain) NSArray*searchData;

 

@property (nonatomic, retain)NSMutableArray *allCitys;

 

@property (nonatomic, retain) UISearchBar*mSearchBar;

@property (nonatomic, retain)UISearchDisplayController *searchController;

 

 

@end

#import"CityListViewController.h"

#import "AppDelegate.h"

- (void)viewDidLoad

{

   //初始化分组表

   self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 88,320, 480) style:UITableViewStyleGrouped];

 

   self.mTableView.delegate = self;

   self.mTableView.dataSource = self;

 

[self.mTableViewsetContentSize:CGSizeMake(320, 3000)];

 

    //初始化搜索条

    self.mSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 44, 320, 44)];

    [self.mSearchBarsetBackgroundImage:[UIImage imageNamed:@"nav_bg.png"]];

   

    [self.mSearchBar setPlaceholder:@"搜索城市"];

 

    self.mSearchBar.delegate = self;

    [self.mSearchBar sizeToFit];

   

    //初始化UISearchDisplayController

    self.searchController =[[UISearchDisplayController alloc] initWithSearchBar:self.mSearchBarcontentsController:self];

    self.searchController.searchResultsDelegate= self;

   self.searchController.searchResultsDataSource = self;

    self.searchController.delegate = self;

 

    //解析数据源文件

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Provineces" ofType:@"plist"];

 

    self.dataList = [NSMutableArrayarrayWithContentsOfFile:path];

 

//确定每个分组的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if([tableViewisEqual:self.searchController.searchResultsTableView]){

        NSLog(@"searchData count is%d",[self.searchData count]);

        return [self.searchData count];

    }

    else{

        NSDictionary *dic = [self.dataListobjectAtIndex:section];

        NSArray *cityCount = [dicobjectForKey:@"Citys"];

       

        int count = (int)[cityCount count];

        for(int i = 0;i<count-1;i++){

            NSDictionary *d = [cityCountobjectAtIndex:i];

            NSString *Name = [dobjectForKey:@"C_Name"];

            [self.allCitys addObject:Name];

        }

 

        return [cityCount count];

    }

}

//分组的个数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    if([tableViewisEqual:self.searchController.searchResultsTableView])

        return 1;

    else

        return [self.dataList count];

}

 

 

 

//每个分组的Header

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    NSString *HeaderName;

    if([tableViewisEqual:self.searchController.searchResultsTableView]){

        HeaderName = @"搜索结果";

    }else{

        NSDictionary *dict = [self.dataListobjectAtIndex:section];

        HeaderName = [dictobjectForKey:@"p_Name"];

        return HeaderName;

    }

    return HeaderName;

}

 

#pragma mark - UISearchDisplayControllerdelegate methods

-(void)filterContentForSearchText:(NSString*)searchText                              scope:(NSString*)scope {

 

    NSMutableArray *searchResult =[[NSMutableArray alloc] initWithCapacity:0];

   

    int j =(int)  [self.allCitys count];

 

    for (int i = 0; i<j-1; i++) {

        NSString *str = [self.allCitysobjectAtIndex:i];

       

        if([strrangeOfString:searchText].location != NSNotFound )

        {

            [searchResult addObject:str];

        }

    }

 

    self.searchData = [NSArrayarrayWithArray:searchResult];

    [searchResult release];

}

 

 

-(BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString {

   

    [selffilterContentForSearchText:searchString

                              scope:[[self.searchDisplayController.searchBar scopeButtonTitles]                                      objectAtIndex:[self.searchDisplayController.searchBar                                                     selectedScopeButtonIndex]]];

   

    return YES;

   

}

 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {

   

    [selffilterContentForSearchText:[self.searchDisplayController.searchBar text]                                scope:[[self.searchDisplayController.searchBar scopeButtonTitles]                                      objectAtIndex:searchOption]];

   

    return YES;

   

}

 

//tableView cell刷新数据

-(UITableViewCell *) tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellName =@"cellName";

   

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellName];

    if(cell == nil)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];

    }

    if([tableViewisEqual:self.searchController.searchResultsTableView]){

        cell.textLabel.text = [self.searchDataobjectAtIndex:indexPath.row];

   

    }else{

        NSDictionary *dict = [self.dataListobjectAtIndex:[indexPath section]];

       

        NSArray *shengfen = [dictobjectForKey:@"Citys"];

       

        NSDictionary *citys = [shengfenobjectAtIndex:indexPath.row];

       

        NSString *Name = [citysobjectForKey:@"C_Name"];

        cell.textLabel.text = Name;

        //[self.allCitys addObject:Name];

    }

    return cell;

                        

                        

}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

    if([tableViewisEqual:self.searchController.searchResultsTableView]){

        self.cityName = [self.searchDataobjectAtIndex:indexPath.row];

        ((AppDelegate *)[[UIApplicationsharedApplication]delegate]).APPDelegateCityName = self.cityName;

    }else{

        self.cityName = [[[[self.dataListobjectAtIndex:indexPath.section] objectForKey:@"Citys"]objectAtIndex:indexPath.row] objectForKey:@"C_Name"];

       

        ((AppDelegate *)[[UIApplicationsharedApplication]delegate]).APPDelegateCityName = self.cityName;

    }

   

 

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

}

 

 

 

posted @ 2013-07-29 19:24  坚固66  阅读(283)  评论(0编辑  收藏  举报