当前位置:编程学习 > wap >>

UISearchDisplayController UISearchBar

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

效果图

 

 

 \\
 

@inte易做图ce 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;

 &

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,