iOS table view 搜索条,谓词的用法

table view 可以说是iPhone开发中最常用的view了。
那么在众多cell中搜索特定关键字就成为了十分有用的方法。

我就在这里简单的示范如何使用UISearchBar 与NSPredicate以完成搜索


// 定义一个用来显示table view数据的数据模型
NSArray *arryStrings = [[NSArray alloc] initWithObjects:@"11", @"12", @"21", @"22", nil]; 

// 定义一个UISearchBar 并将其放置于table view header中
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

// 这句话将在search bar右侧显示cancel按钮
// [searchBar setShowsCancelButton:YES animated:NO];

// 将delegate给自己,以便相应搜索等动作。后面我会详细的写出所有delegate
searchBar.delegate = self;

// 将其放置在header中显示
self.tableView.tableHeaderView = searchBar;

// 你可以加上下面这句话,table view显示的时候会稍微向下滚动一点
// 刚好挡住search bar
[self.tableView scrollRectToVisible:CGRectMake(0, 44, 320, 480) animated:NO];

接下来就是相应用户输入字符时,或者按搜索按钮时的搜索事件。
我这举例子为用户点击键盘上的search按钮时。

// 以下代码写到delegate函数
// - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     
// called when keyboard search button pressed

// 定义一个谓词,方括号里的c意思是忽略大小写。 你也会看到cd就是忽略大小写和发音
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains1 %@", searchBar.text];

// 使用array的方法,用谓词过滤其中的数据(也就是string啦)
[self.arryStrings filterUsingPredicate:pred];

// 用新的数据模型来重新加载table view
[self.tableView reloadData];

// 隐藏键盘
[searchBar resignFirstResponder];

这些是所有的UISearchBarDelegate

@protocol UISearchBarDelegate <NSObject>

@optional

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;                      // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;                        // return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;                       // called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // called before text changes

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;                   // called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;                    // called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2); // called when search results button pressed

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

@end

《iOS table view 搜索条,谓词的用法》有1个想法

  1. In fact, the table view is the controller’s content view, and fills the entire space available to the scene. If you try to drag the table view to the canvas and nothing happens, you’re probably dragging a table view object rather than a table view controller object.

发表评论

电子邮件地址不会被公开。 必填项已用*标注