你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> UISearchDisplayController UISearchBar

UISearchDisplayController UISearchBar

編輯:IOS開發綜合

分組表+本地搜索 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];

}

 

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved