[英]search bar for tableview
I need to add a search bar for a table view.Table contain "Names" which is actually stored in inside an object called "patient".I have an array of "patient" objects. 我需要为表视图添加搜索栏。表包含“名称”,该名称实际上存储在名为“患者”的对象内部。我有一组“患者”对象。 So to setup a search bar to search for names.But how to do it using NSPredicate?
因此要设置一个搜索栏来搜索名称,但是如何使用NSPredicate呢?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* CellId= @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] ;
}
cell.accessoryType = UITableViewCellAccessoryNone;
ObjPatient = [allPatientDetails objectAtIndex:indexPath.row];
cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",ObjPatient.firstName,ObjPatient.lastName];
return cell;
}
Above is the code to show the names on table. 上面的代码在表上显示名称。 Any suggestions are welcome , Thanks
欢迎任何建议,谢谢
To Implement search functionality first take look into below links - 要实现搜索功能,请首先查看以下链接-
https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBar_Class/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html https://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBar_Class/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class /Reference/NSPredicate.html
Then make changes in your code as follows - 然后按如下所示在您的代码中进行更改-
In your .h file declare array - 在您的.h文件中,声明array-
NSArray *filteredArray;
In - (void)viewDidLoad method - 在-(void)viewDidLoad方法-
filteredArray = allPatientDetails;
Then implement UISearchBar delegate method- 然后实现UISearchBar委托方法-
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"SELF.firstName contains[c] %@",
searchText];
NSArray *filteredArray = [allPatientDetails filteredArrayUsingPredicate:predicate];
[tableView reloadData];
}
and make changes in your existing method - 并更改您现有的方法-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* identifier= @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
}
cell.accessoryType = UITableViewCellAccessoryNone;
ObjPatient = [filteredArray objectAtIndex:indexPath.row];
cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",ObjPatient.firstName,ObjPatient.lastName];
return cell;
}
well for nspredicate with custom object can be done like this. 对于带有自定义对象的nspredicate,可以这样完成。
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF.firstName contains[c] %@",
searchText];
self.searchedMemberNameResults = [self.listedMembers filteredArrayUsingPredicate:resultPredicate];
firstName is attribute of an object firstName是对象的属性
Below you can find code for searching an item based on the label in the storyboard. 您可以在下面找到基于情节提要中的标签搜索项目的代码。 It searches dynamically;
它动态搜索; I've tested this and it's working.
我已经对此进行了测试,并且可以正常工作。
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"job_id contains[c] %@",searchText]; searchResultArray = [responceArray filteredArrayUsingPredicate:resultPredicate]; [jobsTable reloadData]; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.