UIKit 框架之UISearchController

//
//  tableViewController.m
//  searchController
//
//  Created by City--Online on 15/6/1.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "tableViewController.h"

@interface tableViewController ()<UISearchResultsUpdating,UISearchControllerDelegate>
@property(nonatomic,strong) NSArray *allData;
@property(nonatomic,strong) NSMutableArray *searchData;
@end

@implementation tableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allData=@[@"123",@"124",@"234",@"256",@"678",@"786",@"12"];
    _searchData=[_allData mutableCopy];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search )];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

}
-(void)search
{
//    为nil时是其本身
    UISearchController *searchvc=[[UISearchController alloc]initWithSearchResultsController:nil];
    searchvc.searchBar.tintColor=[UIColor orangeColor];
    searchvc.searchBar.barTintColor=[UIColor redColor];
    searchvc.definesPresentationContext=YES;
    searchvc.hidesNavigationBarDuringPresentation=NO;
    [searchvc.searchBar sizeToFit];
    searchvc.searchResultsUpdater=self;
    searchvc.delegate=self;
    [self presentViewController:searchvc animated:YES completion:nil];
}

//搜索栏文本内容改变时触发 和下面的
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSLog(@"aaa%@",searchController.searchBar.text);
    if (searchController.searchBar.text.length!=0) {
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchController.searchBar.text];
        _searchData=[[_allData filteredArrayUsingPredicate:predicate] copy];
    }
    [self.tableView reloadData];

}
//搜索视图将要消失时触发
-(void)willDismissSearchController:(UISearchController *)searchController
{

    NSLog(@"%@",searchController.searchBar.text);
    if (searchController.searchBar.text.length!=0) {
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchController.searchBar.text];
        _searchData=[[_allData filteredArrayUsingPredicate:predicate] copy];
    }
    else
    {
         _searchData=[_allData mutableCopy];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _searchData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text=[_searchData objectAtIndex:indexPath.row];

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}

@end

时间: 2024-08-01 03:55:37

UIKit 框架之UISearchController的相关文章

UIKit 框架之UIView二

下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {

UIKit 框架之Bar、Controller

UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarItems,再加上UIViewController.UINavigationController.UITabBarController很容易搞糊涂.我看了好久,没看明白.动手敲了下才有一点感觉. 一.联系 一个UINavigationController对应着一个UINavigationBar.UITo

iOS UIKit 框架 346 篇文档分类整理 - 预告

iOS UIKit 框架 346 篇文档分类整理 - 预告 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分

UIKit框架类层次图

学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

UIKit 框架之UICollectionViewController

1.自定义单元格 #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end #import "myCollectionViewCell.h" @implementa

UIKit 框架之UIView一

- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View's Visual Appearance //配置视觉展示 @property(nonatomic, copy) UIColor *backgroundColor //设置背景色 @property(nonatomic, getter=isHidden) BOOL hidden //隐藏view,默认为NO @property(nonatomic) CGFloat

UIKit 框架之UICollectionView

1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionVi

UIKit框架之NSObject

首先学习NSObject  // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (voi

UIKit 框架之UIScrollView

理解UIScrollView时可以假设这样的一个场景:在一个漆黑的屋子里的墙上有一副大的画,墙是可以上下左右滚动,一个矩形的手电筒固定着,开始时画的左上角坐标与矩形光的左上角坐标对应.人可以用手电筒的光来看这幅画. // // ViewController.m // UIScrollView // // Created by City--Online on 15/5/20. // Copyright (c) 2015年 XQB. All rights reserved. // #import "