• 使用UISearchDisplayController


    使用UISearchDisplayController

    虽然UISearchDisplayController名字中带有controller,可他不是一个UIView相关的controller,因为,切换显示UISearchDisplayController的时候,它并没有在UIWindow中加载(UIWindow一次只能加载一个controller,所以可以证明它不属于controller).

    可以看看,他是继承自NSObject,是多个控件组合在一起的一个东西.

    以下是使用时的效果图:

    源码:

    //
    //  RootViewController.m
    //  TableViewSearch
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
    
    @property (nonatomic, strong) UITableView               *dataView;          // tableView
    @property (nonatomic, strong) UISearchDisplayController *searchController;  // 搜索控制器
    
    @property (nonatomic, strong) NSArray                   *names;             // 数据源
    @property (nonatomic, strong) NSArray                   *searchResults;     // 搜索结果
    
    @end
    
    #define APP_HEIGHT  [UIScreen mainScreen].applicationFrame.size.height
    #define SCR_HEIGHT  [UIScreen mainScreen].bounds.size.height
    #define SCR_WIDTH   [UIScreen mainScreen].bounds.size.width
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 初始化tableView
        _dataView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCR_WIDTH, SCR_HEIGHT - 20)
                                                 style:UITableViewStylePlain];
        _dataView.delegate   = self;
        _dataView.dataSource = self;
        [self.view addSubview:_dataView];
        
        // 初始化searchBar
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        searchBar.delegate     = self;
        searchBar.placeholder  = @"搜索";
        [searchBar sizeToFit];
        
        // 将searchBar放置在tableView的HeaderView中
        self.dataView.tableHeaderView = searchBar;
    
        // 初始化search控制器并获取searchBar以及当前视图控制器
        _searchController = 
            [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                              contentsController:self];
        _searchController.delegate                = self;
        _searchController.searchResultsDataSource = self;
        _searchController.searchResultsDelegate   = self;
        
        // tableView的数据源
        _names = @[@"Aaliyah", @"Aaron", @"Abigail", @"Adam", @"Addison", @"Adrian", @"Aiden", @"Alex", @"Alexa", @"Alexander", @"Alexandra", @"Alexis", @"Allison", @"Alyssa", @"Amelia", @"Andrea", @"Andrew", @"Angel", @"Anna", @"Annabelle", @"Anthony", @"Aria", @"Ariana", @"Arianna", @"Ashley", @"Aubree", @"Aubrey", @"Audrey", @"Austin", @"Autumn", @"Ava", @"Avery", @"Ayden", @"Bailey", @"Bella", @"Benjamin", @"Bentley", @"Blake", @"Brandon", @"Brayden", @"Brianna", @"Brody", @"Brooklyn", @"Bryson", @"Caleb", @"Cameron", @"Camila", @"Carlos", @"Caroline", @"Carson", @"Carter", @"Charles", @"Charlotte", @"Chase", @"Chloe", @"Christian", @"Christopher", @"Claire", @"Colton", @"Connor", @"Cooper", @"Damian", @"Daniel", @"David", @"Dominic", @"Dylan", @"Easton", @"Eli", @"Elijah", @"Elizabeth", @"Ella", @"Ellie", @"Emily", @"Emma", @"Ethan", @"Eva", @"Evan", @"Evelyn", @"Faith", @"Gabriel", @"Gabriella", @"Gavin", @"Genesis", @"Gianna", @"Grace", @"Grayson", @"Hailey", @"Hannah", @"Harper", @"Henry", @"Hudson", @"Hunter", @"Ian", @"Isaac", @"Isabella", @"Isaiah", @"Jace", @"Jack", @"Jackson", @"Jacob", @"James", @"Jasmine", @"Jason", @"Jaxon", @"Jayden", @"Jeremiah", @"Jocelyn", @"John", @"Jonathan", @"Jordan", @"Jose", @"Joseph", @"Joshua", @"Josiah", @"Juan", @"Julia", @"Julian", @"Justin", @"Katherine", @"Kayden", @"Kayla", @"Kaylee", @"Kennedy", @"Kevin", @"Khloe", @"Kimberly", @"Kylie", @"Landon", @"Lauren", @"Layla", @"Leah", @"Levi", @"Liam", @"Lillian", @"Lily", @"Logan", @"London", @"Lucas", @"Lucy", @"Luis", @"Luke", @"Lydia", @"Mackenzie", @"Madeline", @"Madelyn", @"Madison", @"Makayla", @"Mason", @"Matthew", @"Maya", @"Melanie", @"Mia", @"Michael", @"Molly", @"Morgan", @"Naomi", @"Natalie", @"Nathan", @"Nathaniel", @"Nevaeh", @"Nicholas", @"Noah", @"Nolan", @"Oliver", @"Olivia", @"Owen", @"Parker", @"Peyton", @"Piper", @"Reagan", @"Riley", @"Robert", @"Ryan", @"Ryder", @"Samantha", @"Samuel", @"Sarah", @"Savannah", @"Scarlett", @"Sebastian", @"Serenity", @"Skylar", @"Sofia", @"Sophia", @"Sophie", @"Stella", @"Sydney", @"Taylor", @"Thomas", @"Trinity", @"Tristan", @"Tyler", @"Victoria", @"Violet", @"William", @"Wyatt", @"Xavier", @"Zachary", @"Zoe", @"Zoey"];
    }
    
    //===============================================
    #pragma mark -
    #pragma mark - tableView相关代理
    //===============================================
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.dataView)
        {
            return [self.names count];
        }
        else
        {
            return [self.searchResults count];
        }
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath
                                 animated:YES];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *reusedStr = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:reusedStr];
        }
        
        if (tableView == self.dataView)
        {
            cell.textLabel.text = self.names[indexPath.row];
        }
        else
        {
            cell.textLabel.text = self.searchResults[indexPath.row];
        }
        
        return cell;
    }
    
    //===============================================
    #pragma mark -
    #pragma mark UISearchDisplayController相关代理
    //===============================================
    
    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        NSLog(@"将要开始搜索");
    }
    
    - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
    {
        NSLog(@"开始搜索");
    }
    
    - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
    {
        NSLog(@"将要结束搜索");
    }
    
    - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
    {
        NSLog(@"结束搜索");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
      didLoadSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"加载了tableView");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
    willUnloadSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"卸载了tableView");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
     willShowSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"将要显示tableView");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
      didShowSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"已经显示了tableView");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
     willHideSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"将要隐藏tableView");
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller
      didHideSearchResultsTableView:(UITableView *)tableView
    {
        NSLog(@"已经隐藏了tableView");
    }
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller
    shouldReloadTableForSearchString:(NSString *)searchString
    {
        NSLog(@"要重新加载tableView显示搜索的数据么?");
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];
        self.searchResults = [self.names filteredArrayUsingPredicate:predicate];
        
        return YES;
    }
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller
    shouldReloadTableForSearchScope:(NSInteger)searchOption
    {
        NSLog(@"要重新加载tableView的搜索域么?");
        return YES;
    }
    
    @end

    UISearchDisplayController自带了tableView,也是走的代理方法,与你的controller中的tableView一样的代理方法,这点需要注意:

  • 相关阅读:
    axios、ajax、fetch三者的区别
    React与Vue的相同与不同点
    react-redux
    redux【react】
    react高阶组件
    基于WebGL无插件虚拟场景漫游关键技术(完整版)ThingJS
    基于WebGL的三维交通监控可视化技术应用(实践版) ThingJS
    地下管线监控系统中互联网WebGL三维可视化构建技术 ThingJS
    基于WebGL实现智慧校园的全景漫游技术研究 三维可视化
    基于WebGL的3D可视化告警系统关键技术解析 ThingJS
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3816660.html
Copyright © 2020-2023  润新知