UIKit 框架之UISearchController
2024-09-25 09:56:39
// // 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=@["]; _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); ) { 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); ) { 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 { ; } - (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
最新文章
- NOI 1.5 42:画矩形
- NuGet在创建pack时提示”The replacement token &#39;author&#39; has no value“问题解决
- spark的安装
- Struts2(十五)实现文件上传
- python4delphi Python could not be properly initialized. We must quit.
- FOOD
- [iOS基础控件 - 6.9.4] 抓取网页图片资源
- java保留两位小数的方法
- AIX-df命令
- C/C++输入两个任意日期求相隔天数
- 查看Zookeeper服务器状态信息的一些命令
- C#的dapper使用
- Laravel 入口文件解读及生命周期
- CPU上下文切换
- jsp实现验证码登陆
- (Android数据传递)Service和Activity之间-- 借助BroadcastReceiver--的数据传递
- 纯CSS下拉菜单(希望对有需要的小伙伴有所帮助)
- (转)SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间
- 【Python】批量爬取网站URL测试Struts2-045漏洞
- SocketIOCP