zoukankan      html  css  js  c++  java
  • 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];

    }

  • 相关阅读:
    让超链接点击后不跳转,可以用href = "#",但是这个#就会锚点到页面最上边 点击链接后不跳转可以设置成
    js 小数取整的函数
    谷歌浏览器常用快捷键
    Vi问题
    UbuntuFAQ
    Ubuntu下配置C/C++开发环境
    win7硬盘安装ubuntu双系统——注意项
    怎样判断自己是否在平庸者之列?
    2012年软件开发者薪资调查报告
    VIM常用快捷键~网页上查找
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3223547.html
Copyright © 2011-2022 走看看