iOS8之后搜索框的常规实例
1、在一个二级导航控制器中添加一个UITableviewController作为子控制器
2、UITableviewController.tableView 作为展示结果
3、利用iOS之后的UISearchController 根据输入更新输入结果
@interface WJWSearchViewController ()<UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableViewController *searchTableViewController;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *searchList;
@end
@implementation WJWSearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self addChildViewController:self.searchTableViewController];
[self initDataArray];
[self configUI];
}
- (void)initDataArray{
[self.dataArray addObject:@"runLoop"];
[self.dataArray addObject:@"gcd"];
[self.dataArray addObject:@"timer"];
[self.dataArray addObject:@"sideBar"];
[self.dataArray addObject:@"searchBar"];
[self.dataArray addObject:@"fmdb"];
[self.dataArray addObject:@"afnetworking"];
}
- (void)configUI {
[self.view addSubview: self.searchTableViewController.tableView];
self.searchTableViewController.tableView.frame = self.view.frame;
}
#pragma mark ---UITableViewDataSource---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.active) {
return [self.searchList count];
}else {
return self.dataArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchCellId = @"SearchCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchCellId];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}else {
[cell.textLabel setText:self.dataArray[indexPath.row]];
}
return cell;
}
#pragma mark ----UISearchResultsUpdating----
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList != nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
//刷新表格
[self.searchTableViewController.tableView reloadData];
}
-(UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = YES;//搜索框编辑时隐藏导航栏
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
self.searchTableViewController.tableView.tableHeaderView = _searchController.searchBar;
}
return _searchController;
}
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (UITableViewController *)searchTableViewController {
if (!_searchTableViewController) {
_searchTableViewController = [[UITableViewController alloc] init];
_searchTableViewController.tableView.delegate = self;
_searchTableViewController.tableView.dataSource = self;
}
return _searchTableViewController;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
遇到的问题:
模拟器无法中文输入,
解决方法:
1、在Xcode中给项目的Scheme设置目标项目的 所属区域。默认一般使系统,可以设置为china。edit scheme-> option ->application region :china
2、在模拟器中的通用->语言地区->iPhone : 简体中文
思考:关于拼音搜索,首字母搜索,等模糊搜索未实现。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
2018-04-18 xCode 升级9.3之后巨卡