美文网首页谓词正则iOS
IOS-模糊搜索UISearchBar+UISearchDisp

IOS-模糊搜索UISearchBar+UISearchDisp

作者: bingo居然被占了 | 来源:发表于2016-09-02 21:10 被阅读728次
#import "RootViewController.h"

@interface RootViewController ()< UITableViewDataSource , UITableViewDelegate , UISearchDisplayDelegate , UISearchBarDelegate >

@property ( nonatomic , strong ) NSArray *dataArr; // 数据源

@property ( nonatomic , strong ) NSArray *resultsArr; // 搜索结果

@property ( nonatomic , strong ) UISearchBar *search;

@property ( nonatomic , strong ) UISearchDisplayController * searchPlay;

@property ( nonatomic , strong ) UITableView *aTableView;

@end

@implementation RootViewController

- ( id )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil

{

self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];

if ( self ) {

// Custom initialization

}

return self ;

}

- ( void )viewDidLoad

{

[ super viewDidLoad ];

self . title = @" 搜索 " ;

_dataArr = [[ NSArray alloc ] initWithObjects : @"aaa" , @"abc" , @"aqq" , @"bdc" , @"gcd" , @"mnb" , @"zzz" , nil ];

[ self createView ];

// Do any additional setup after loading the view.

}

- ( void ) createView

{

_aTableView = [[ UITableView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 480 )];

_aTableView . delegate = self ;

_aTableView . dataSource = self ;

[ self . view addSubview : _aTableView ];

}

#pragma mark -

#pragma mark UITableViewDelegate

-( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:( NSInteger )section

{

_search = [[ UISearchBar alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 40 )];

_search . backgroundColor = [ UIColor redColor ];

_search . delegate = self ;

_search . showsCancelButton = YES ;

_search . keyboardType = UIKeyboardTypeDefault ;

_searchPlay = [[ UISearchDisplayController alloc ] initWithSearchBar : self . search contentsController : self ];

_searchPlay . delegate = self ;

_searchPlay . searchResultsDataSource = self ;

_searchPlay . searchResultsDelegate = self ;

_searchPlay . active = NO ;

return _searchPlay . searchBar ;

}

- ( BOOL )searchBarShouldEndEditing:( UISearchBar *)searchBar

{

NSLog ( @" 取消 " );

return YES ;

}

- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section

{

return   tableView == self . searchPlay . searchResultsTableView ? 0 : 40 ;

}

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

NSInteger row = 0 ;

if ([tableView isEqual : self . searchPlay . searchResultsTableView ]) {

row = [ self . resultsArr count ];

} else {

row = [ self . dataArr count ];

}

return row;

}

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

{

static NSString *CellIdentifier = @"Cell" ;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :CellIdentifier];

if (!cell) {

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

}

if ([tableView isEqual : self . searchPlay . searchResultsTableView ]) {

cell. textLabel . text = [ self . resultsArr objectAtIndex :indexPath. row ];

} else {

cell. textLabel . text = [ self . dataArr objectAtIndex :indexPath. row ];

}

return cell;

}

#pragma mark -

#pragma mark UISearchDisplayControllerDelegate

//text 是输入的文本 ,scope 是搜索范围

- ( void ) searchText:( NSString *)text andWithScope:( NSString *)scope

{

//CONTAINS 是字符串比较操作符,

NSPredicate *result = [ NSPredicate predicateWithFormat : @"SELF contains[cd] %@" ,text];

self . resultsArr = [ self . dataArr filteredArrayUsingPredicate :result];

}

- ( BOOL ) searchDisplayController:( UISearchDisplayController *)controller shouldReloadTableForSearchString:( NSString *)searchString

{

// searchString 是输入的文本

// 返回结果数据 读取搜索范围 在选择范围

NSArray *searScope = [ self . searchPlay . searchBar scopeButtonTitles ]; // 数组范围

[ self searchText :searchString andWithScope :[searScope objectAtIndex :[ self . searchPlay . searchBar selectedScopeButtonIndex ]]];

return YES ;

}

- ( BOOL ) searchDisplayController:( UISearchDisplayController *)controller shouldReloadTableForSearchScope:( NSInteger )searchOption

{

NSString *inputText = self . searchPlay . searchBar . text ; // 搜索输入的文本

NSArray *searScope = [ self . searchPlay . searchBar scopeButtonTitles ]; // 索索范围

[ self searchText :inputText andWithScope :[searScope objectAtIndex :searchOption]];

return YES ;

}

@end

相关文章

  • IOS-模糊搜索UISearchBar+UISearchDisp

  • iOS-搜索框 模糊查询 1 (UISearchControll

    一、模糊查询 第一种方法:UISearchController 第二种方法:UISearchBar 二、UISea...

  • 模糊搜索

    【法1】Android基础控件—SearchView 【参考链接】 http://blog.csdn.net/za...

  • 模糊搜索

    http://blog.csdn.net/lianbaixue/article/details/10579117

  • iOS-高斯模糊

    前言 昨天看到一位简书作者的博客,写的是swift的版的模糊图,正好我做的项目有这个需求,所以就翻译了一版OC语言...

  • iOS-模糊查询

    前言: 为了巩固FMDB,就来找个简单的Demo学习一下。不好找工作啊,就学习吧,没应聘的消遣吧。 简单介绍:模糊...

  • iOS-模糊效果

    FXBlurView 场景是banner中三张图片 因为图片大小不一致 所以加一个模糊 参考FXBlurView属...

  • 八、模糊搜索

    jsp代码: servlet代码:

  • Android 模糊搜索

    在Android移动端开发过程中,列表展示是咱们经常使用的一种展现方式。这个时候就可能有如下情况出现了,比如...

  • iOS 模糊搜索

    [NSPredicate predicateWithFormat: @"shiche_nickname CONTA...

网友评论

    本文标题:IOS-模糊搜索UISearchBar+UISearchDisp

    本文链接:https://www.haomeiwen.com/subject/sjlkettx.html