iOS学习笔记之UI-UISearchController-And-UITableView
来源:互联网 发布:淘宝拉夏贝尔代购 编辑:程序博客网 时间:2024/06/04 18:28
//前言:从java转到iOS各种心酸苦辣,这里全都是坑阿//今天学到表视图与搜索框,觉得有必要与大家分享一下,这个小demo是 纯手写没有storyboard入口,是调用window创建的controller- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[ViewController alloc] init]; [self.window makeKeyAndVisible]; return TRUE;}//tableView实现的两个协议 UITableViewDataSource,UITableViewDelegate //UISearchController实现的两个协议 _searchController.searchResultsUpdater = self; _searchController.searchBar.delegate = self;#import "ViewController.h"#define cellHight 90@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>//搜索控制器@property (strong, nonatomic)UISearchController *searchController;//所有数据@property (strong,nonatomic)NSArray *listTeams;//筛选过的数据@property (strong, nonatomic)NSMutableArray *listFilterTeams;//筛选方法- (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //获取所有数据 _listTeams = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"]]; //初始化数据 [self filterContentForSearchText:@"" scope:-1]; //初始化控制器 _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; //设置搜索框为灰色 _searchController.dimsBackgroundDuringPresentation = false; //设置搜索分类 _searchController.searchBar.scopeButtonTitles = @[@"中文",@"英文"]; //设置searchResultsUpdater委托代理 _searchController.searchResultsUpdater = self; //设置searchBarDelegate委托代理 _searchController.searchBar.delegate = self; //在表示图头部添加搜索框 self.tableView.tableHeaderView = _searchController.searchBar; [_searchController.searchBar sizeToFit]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark --内容过滤- (void)filterContentForSearchText:(NSString *)searchText scope:(NSInteger)scope{ if (searchText.length == 0) { _listFilterTeams = [NSMutableArray arrayWithArray:_listTeams]; return; } //持久化查询 NSPredicate *predicte; //筛选后临时数组 NSArray *teamArray; switch (scope) { case 0: //包含查询 predicte = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText]; teamArray = [_listTeams filteredArrayUsingPredicate:predicte]; _listFilterTeams = [NSMutableArray arrayWithArray:teamArray]; break; case 1: predicte = [NSPredicate predicateWithFormat:@"SELF.nameEnglish contains[c] %@",searchText]; teamArray = [_listTeams filteredArrayUsingPredicate:predicte]; _listFilterTeams = [NSMutableArray arrayWithArray:teamArray]; break; default: _listFilterTeams = [NSMutableArray arrayWithArray:_listTeams]; break; }}#pragma mark --协议方法 返回row- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //返回表行数 return _listFilterTeams.count;}#pragma mark --创建单元表cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //创建单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search"]; if (cell == nil) { //初始化单元格 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"search"]; } //获取数据 NSDictionary *rowDiction = _listFilterTeams[indexPath.row]; cell.textLabel.text = rowDiction[@"name"]; cell.detailTextLabel.text = rowDiction[@"intro"]; cell.imageView.image = [UIImage imageNamed:rowDiction[@"icon"]]; //使副标题自动换行 cell.detailTextLabel.numberOfLines = 0; //设置表单元样式 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;}UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"search"];这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。#pragma mark --实现刷新协议- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ [self updateSearchResultsForSearchController:_searchController];}#pragma mark --实时刷新- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ [self filterContentForSearchText:searchController.searchBar.text scope:searchController.searchBar.selectedScopeButtonIndex]; //重新执行table相关协议函数 [self.tableView reloadData];}@end
1 0
- iOS学习笔记之UI-UISearchController-And-UITableView
- IOS UI学习之UITableView
- iOS笔记UI--UITableView
- 【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)
- [iOS学习]之UISearchController简单使用
- Swift UI学习之UITableView and protocol use
- Swift UI学习之UITableView and protocol use
- Swift UI学习之UITableView and protocol use
- ioS开发之UI基础--UITableView简单了解(个人笔记)
- iOS学习笔记之UITableView(1)
- 初入IOS之UI控件学习 UITableView
- iOS开发之UI学习-UITableView的复用机制
- IOS UITableView 学习笔记
- iOS 学习笔记 uitableView
- IOS学习之UITableView
- IOS学习之 UITableView
- IOS学习之UITableView
- iOS学习笔记之UITableView之右侧索引
- 自定义RatingBar
- K60学习笔记五:串行通信的时序分析
- [CodeVs][1514][Treap][书架]
- Windows x64 Android开发环境搭建
- Java_SE05-Comparable,Map接口,队列,栈
- iOS学习笔记之UI-UISearchController-And-UITableView
- 给图像添加边界
- mysql 事务
- Handlebars.js 使用入门
- 在命令提示符窗口进入MYSQL数据库的方法(转)
- MultipleRegularExpressionAttribute MVC中扩展自定义验证规则
- Android知识总结:ListView 出现IndexOutOfBoundsException异常
- elgg安装
- Swift编程基础(二):字符与字符串