php实现联想搜索,搜索框联想功能实现

#import "GHSearchController.h"

@interface GHSearchController ()

@property (nonatomic, strong) UISearchController *searchVC;

@property (nonatomic ,strong)UITableView *demoTableView;

@property (nonatomic ,strong) NSMutableArray *exampleArr;

@property (nonatomic ,strong)NSMutableArray *searchArr;

@end

@implementation GHSearchController

#pragma mark - 懒加载

- (UISearchController *)searchVC

{

if (!_searchVC) {

_searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];

_searchVC.searchResultsUpdater = self;

_searchVC.dimsBackgroundDuringPresentation = NO;

_searchVC.hidesNavigationBarDuringPresentation = NO;

_searchVC.searchBar.frame = CGRectMake(self.searchVC.searchBar.frame.origin.x, self.searchVC.searchBar.frame.origin.y, self.searchVC.searchBar.frame.size.width, 44.0);

self.demoTableView.tableHeaderView = self.searchVC.searchBar;

}

return _searchVC;

}

- (NSMutableArray *)exampleArr

{

if (!_exampleArr) {

}

return _exampleArr;

}

- (UITableView *)demoTableView

{

if (!_demoTableView) {

_demoTableView = [[UITableView alloc] init];

_demoTableView.dataSource = self;

}

return _demoTableView;

}

#pragma mark - view

- (void)viewDidLoad

{

[super viewDidLoad];

self.view = self.demoTableView;

_exampleArr = [NSMutableArray arrayWithCapacity:200];

CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];

self.demoTableView.frame = CGRectMake(0, rectStatus.size.height, self.view.frame.size.width, self.view.frame.size.height - rectStatus.size.height);

// self.searchBar.frame = CGRectMake(0, rectStatus.size.height, self.view.frame.size.width, 50.0);

for (int i = 0; i < 200; i ++) {

int NUMBER_OF_CHARS = 5;

char data[NUMBER_OF_CHARS];//生成一个五位数的字符串

for (int x=0;x<10;data[x++] = (char)('A' + (arc4random_uniform(26))));

NSString *string = [[NSString alloc] initWithBytes:data length:5 encoding:NSUTF8StringEncoding];//随机给字符串赋值

[_exampleArr addObject:string];

} // 随机生成200个五位数的字符串

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//cell

{

static NSString *identify = @"cellIdentify";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];

}

if (!self.searchVC.active) {

cell.textLabel.text = [NSString stringWithFormat:@"%@",_exampleArr[indexPath.row]];

}else

{

cell.textLabel.text = [NSString stringWithFormat:@"%@",_searchArr[indexPath.row]];

}

return cell;

}

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

{

if (self.searchVC.active) {

return self.searchArr.count;//搜索结果

}else

{

return self.exampleArr.count;//原始数据

}

}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

NSString *searchString = [self.searchVC.searchBar text];

NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];

if (self.searchArr!= nil) {

[self.searchArr removeAllObjects];

}

//过滤数据

self.searchArr = [NSMutableArray arrayWithArray:[_exampleArr filteredArrayUsingPredicate:preicate]];

//刷新表格

[self.demoTableView reloadData];

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值