Xcode UISearchBar代码实现

xcodenab

我目前正在研究一个项目,该项目必须通过NSArray(显示在UITableView中)进行过滤。问题是,既没有XIB文件也没有情节提要。我的问题是(已经设置了谓词和搜索方法et.c),如何仅使用代码使SearchBar出现在程序中-并且我需要能够使用它。对于这个问题的帮助,我感到非常高兴,因为我发现的每个教程都使用情节提要或xibs :(

非常感谢您!顺便说一下,这是我到目前为止有关搜索栏的两种方法,TableView也已经设置好了。

-(void) filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
self.searchResults = [self.array filteredArrayUsingPredicate:predicate];

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString  {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;

}

阿维斯马拉

您可以执行以下操作:

在您的.h中添加以下内容:

@property (nonatomic,strong) UISearchBar *searchBar;

在您的viewDidLoad方法中添加

self.searchBar = [[UISearchBar alloc]init];
self.searchBar.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
self.searchBar.delegate = self;
//Customize the searchBar
[self.view addSubview:self.searchBar];

而且,每当您要搜索时,请按键盘内的搜索按钮。执行此操作时,将调用一个委托。像这样

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  [searchBar resignFirstResponder];
  //Do some search
}

您可以使用searchBar.text属性访问输入的文本我假设您已经实施了相关协议。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章