UISearchController
来源:互联网 发布:人在德国上淘宝 编辑:程序博客网 时间:2024/05/15 01:40
//// LJESearchController.m// LJESearchController//// Created by lije on 15/6/29.// Copyright (c) 2015年 lije. All rights reserved.//#import "LJESearchController.h"#define kRowHeight 60#define kSearchBarHeight 80#define kSearchBarWidth self.view.frame.size.width@interface LJESearchController ()<UISearchResultsUpdating> { UITableView *_tableview;}@property (nonatomic, strong) NSArray *searchArr; // 保存搜索过的数据@property (nonatomic, strong) NSArray *dataSource; // 保存原有数据@property (nonatomic, strong) UISearchController *searchController;@end@implementation LJESearchControllerNSString * const CellIdentifier = @"CellIdentifier";- (void)viewDidLoad { [super viewDidLoad]; self.title = @"Search"; [self configureLayout];}- (void)viewWillAppear:(BOOL)animated { if (!_searchArr) { return; } [_tableview reloadData];}- (void)configureLayout { _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier]; _tableview.delegate = self; _tableview.dataSource = self; [self.view addSubview:_tableview]; // 创建搜索条 self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; // 设置字体渲染颜色 self.searchController.searchBar.tintColor = [UIColor orangeColor]; // 设置状态条颜色 self.searchController.searchBar.barTintColor = [UIColor orangeColor]; // 设置开始搜索的时候是否还显示背景颜色 self.searchController.dimsBackgroundDuringPresentation = YES; // 让searchBar自适应屏幕大小 [self.searchController.searchBar sizeToFit]; // 设置显示搜索结果的控制器 self.searchController.searchResultsUpdater = self; _tableview.tableHeaderView = self.searchController.searchBar;}#pragma mark - UITableviewDelegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.searchArr.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return kRowHeight;}#pragma mark - UITableviewDataSource- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if (indexPath.row < self.searchArr.count) { cell.textLabel.text = self.searchArr[indexPath.row]; } return cell;}#pragma mark - UISearchResultsUpdating- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@", searchController.searchBar.text]; self.searchArr = [NSMutableArray arrayWithArray:[self.dataSource filteredArrayUsingPredicate:predicate]]; [_tableview reloadData];}#pragma mark - 懒加载 runtime- (NSArray *)dataSource { if (!_dataSource) { self.dataSource = _dataSource = @[@"wangyu", @"zhangjing", @"mali"]; } return _dataSource;}- (NSArray *)searchArr { if (!_searchArr) { // 如果searchArr为空则把原始的数据传给它 self.searchArr = self.dataSource; } return _searchArr;}@end
0 0
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UIsearchController
- UIsearchController
- UISearchController
- UISearchController
- UISearchController
- iOS UISearchController
- iOS - UISearchController
- UISearchController偏移
- UISearchController搜索
- iOS UISearchController的使用
- Objective-C之UISearchController
- OCUI界面设计:UISearchController 搜索
- 玩命牛的成长记录(二十三)——各种文字特效
- java中的线程初步
- Apple硬件设备占有率统计网站
- Android之Monkey全参数(包含隐藏参数)
- [华为机试练习题]17.可怕的阶乘
- UISearchController
- 《猜猜看》之参考“单选按钮事件处理”
- MySQL数据库主从同步
- android 即时通讯 心跳
- Android图片合成/裁剪原理(转)
- Lua笔记
- 谨慎使用A2W等字符转换宏
- Python seek函数
- Android的APK升级以及下载安装