如何搜索词典的数组和显示的UITableView? [英] How to Search Array of Dictionary and show in UITableview?
本文介绍了如何搜索词典的数组和显示的UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在IOS pretty新我使用 UISearchDisplayController
进行搜索。
- (BOOL)searchDisplayController:(UISearchDisplayController *)控制器shouldReloadTableForSearchString:(* NSString的)搜索字符串
{
[个体经营filterContentForSearchText:搜索字符串
范围:[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]]; 返回YES;
} - (无效)filterContentForSearchText:(* NSString的)SEARCHTEXT范围:(* NSString的)范围
{
NS predicate * predicate = NS predicate predicateWithFormat:@任何自我==%@,SEARCHTEXT]
NSArray的*过滤= [self.arrProductList filteredArrayUsing predicate:predicate]。
的NSLog(@%@,过滤);
}
这是我的self.arrProductList是数组的数组即
({
ID = 1;
说明=合并运作急躁T恤
PNAME =凝聚正常工作的T恤
价格=299.00;
数量= 99;
},
{
ID = 2;
说明=噬魂Krylon Bombear打掉T恤
PNAME =少女的T恤
价格=499.00;
数量= 99;
},
{
ID = 3;
说明=一开始孩子们向上波段营套头衫帽衫
PNAME =乐队营T恤
价格=399.00;
数量= 99;
})
我的问题是如何搜索使用keyPNAME?我的应用程序在崩溃
filteredArrayUsing predicate:
块引用>解决方案我的朋友解决了这个问题。这里,是code:
- (无效)searchBarDidBeginEditing:(*的UISearchBar)lclSearchBar
{
self.searchBar.showsCancelButton = YES;
}
的#pragma马克 - UISearchDisplayController委托方法
- (BOOL)searchDisplayController:(UISearchDisplayController *)控制器shouldReloadTableForSearchString:(* NSString的)搜索字符串
{ [个体经营filterContentForSearchText:搜索字符串
范围:[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]]; 返回YES;
} - (无效)filterContentForSearchText:(* NSString的)SEARCHTEXT范围:(* NSString的)范围
{
如果(searchResults.count!= 0){
[SearchResult所removeAllObjects]
}
的for(int i = 0; I< self.arrProductList计数];我++)
{
* NSString的字符串= [[self.arrProductList objectAtIndex:我] valueForKey:@PNAME];
NSRange rangeValue = [字符串rangeOfString:SEARCHTEXT选项:NSCaseInsensitiveSearch];
如果(rangeValue.length大于0)
{
的NSLog(@字符串包含喇嘛!); [SearchResult所ADDOBJECT:[self.arrProductList objectAtIndex:我]];
}
其他
{
的NSLog(@字符串不包含喇嘛);
}
}
的NSLog(@fiilterArray:%@,SearchResult所);
}I am pretty new in IOS and I am using
UISearchDisplayController
for searching.-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF == %@", searchText]; NSArray *filtered = [self.arrProductList filteredArrayUsingPredicate:predicate]; NSLog(@"%@", filtered); }
And Here is my self.arrProductList is an array of array i.e
({ ID = 1; description = "Coalesce Functioning on impatience T-Shirt"; pname = "Coalesce Functioning T-Shirt"; price = "299.00"; qty = 99; }, { ID = 2; description = "Eater Krylon Bombear Destroyed T-Shirt"; pname = "Girl's T-Shirt"; price = "499.00"; qty = 99; }, { ID = 3; description = "The Get-up Kids Band Camp Pullover Hoodie"; pname = "Band Camp T-Shirt"; price = "399.00"; qty = 99; })My question is how to search using key "pname"? My app is Crashed in
filteredArrayUsingPredicate:
解决方案My friend solve this issue. Here, is the code:
- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar { self.searchBar.showsCancelButton = YES; } #pragma mark - UISearchDisplayController delegate methods -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { if (searchResults.count != 0) { [searchResults removeAllObjects]; } for (int i=0; i< [self.arrProductList count]; i++) { NSString *string = [[self.arrProductList objectAtIndex:i] valueForKey:@"pname"]; NSRange rangeValue = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; if (rangeValue.length > 0) { NSLog(@"string contains bla!"); [searchResults addObject:[self.arrProductList objectAtIndex:i]]; } else { NSLog(@"string does not contain bla"); } } NSLog(@"fiilterArray : %@",searchResults); }
这篇关于如何搜索词典的数组和显示的UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文