• UI1_UITableViewSearchController


    //  UI1_UITableViewSearchController
    //
    //  Created by zhangxueming on 15/7/15.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "ViewController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        ViewController *root = [[ViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
        self.window.rootViewController = nav;
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeK
    //
    //  ViewController.m
    //  UI1_UITableViewSearchController
    //
    //  Created by zhangxueming on 15/7/15.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        UITableView *_tableView;//表视图
        NSMutableArray *_dataList;//数据源
        //UISearchDisplayController
        UISearchController *_searchController;//搜索控制器
        NSMutableArray *_searchList;//搜索的结果
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self createData];
        [self createUI];
    }
    
    - (void)createData
    {
        _dataList = [NSMutableArray array];
        _searchList = [NSMutableArray array];
        for (int i='A'; i<='Z'; i++) {
            NSMutableArray *array = [NSMutableArray array];
            for (int j=0; j<5; j++) {
                NSInteger num = arc4random()%50+1;
                NSString *name = [NSString stringWithFormat:@"人物%d:name%ld",j,num];
                [array addObject:name];
            }
            [_dataList addObject:array];
        }
    }
    
    - (void)createUI
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource  = self;
        [self.view addSubview:_tableView];
        
        //创建搜索控制器, nil 表示在当前view上显示搜索结果
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        //更新搜索结果
        _searchController.searchResultsUpdater = self;
        //隐藏导航栏
        _searchController.hidesNavigationBarDuringPresentation = NO;
        //背景变暗
        _searchController.dimsBackgroundDuringPresentation = NO;
        //添加searchBar
        CGRect frame = _searchController.searchBar.frame;
        frame.size.height = 44;
        _searchController.searchBar.frame = frame;
        _tableView.tableHeaderView = _searchController.searchBar;
    }
    
    #pragma mark  ---UITableView代理---
    
    //返回分区的个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        if(_searchController.active)
        {
            return 1;
        }
        else
        {
            return _dataList.count;
        }
    }
    //返回分区中的行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //_dataList[section] <==> [_dataList objectAtIndex:section];
        if (_searchController.active) {
            return _searchList.count;
        }
        return [_dataList[section] count];
    }
    
    //创建cell
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellId = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        }
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (_searchController.active) {
            cell.textLabel.text = _searchList[indexPath.row];//[_searchList objectAtIndex:indexPath.row];
        }
        else
        {
            cell.textLabel.text = _dataList[indexPath.section][indexPath.row];
        }
        return cell;
    }
    
    //设置分区的头标题
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if (_searchController.active) {
            return @"搜索结果";
        }
        return [NSString stringWithFormat:@"%c",(char)(section+'A')];
    }
    //设置分区头的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 50;
    }
    
    //设置分区索引
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        if(_searchController.active)
        {
            return nil;
        }
        NSMutableArray *titles = [NSMutableArray array];
        [titles addObject:UITableViewIndexSearch];
        for (int i='A'; i<='Z'; i++) {
            [titles addObject:[NSString stringWithFormat:@"%c",(char)i]];
        }
        return titles;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
        return index-1;
    }
    
    //更新搜索结果
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
    {
        [_searchList removeAllObjects];
        //NSPredicate 谓词语法
        //过滤数据
        NSString *searchString = _searchController.searchBar.text;
        for (int i=0; i<_dataList.count; i++) {
            NSInteger count = [[_dataList objectAtIndex:i] count];
            for (NSInteger j=0; j<count; j++) {
                NSString *obj = [[_dataList objectAtIndex:i] objectAtIndex:j];
                if ([obj containsString:searchString]) {
                    [_searchList addObject:obj];
                }
            }
        }
        [_tableView reloadData];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    eyAndVisible]; return YES; } // // ViewController.h // UI1_UITableViewSearchController // // Created by zhangxueming on 15/7/15. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource,UISearchResultsUpdating> @end
  • 相关阅读:
    N25_复杂链表的复制
    N24_二叉树中和为某一路径
    N23_判断是否为二叉搜索树的后序遍历序列
    N22_从上到下打印二叉树
    win7桌面小工具已停止工作解决办法
    C3P0数据库连接池使用
    js中的页面跳转
    怎么用js代码禁止浏览器的前进与后退?
    怎么在 Dos 下运行 PHP 和 MySQL 命令
    80端口被system 占用解决方法
  • 原文地址:https://www.cnblogs.com/0515offer/p/4649136.html
Copyright © 2020-2023  润新知