我有一个带有搜索栏的tableview。当我将项目添加到另一个数组时,我能够使用NSPredicate搜索tableview:
for(head in items){
[desc addObject:head.DESC];
[category addObject:head.CATEGORY];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];
searchResults = [desc filteredArrayUsingPredicate:resultPredicate];
}
现在我想将集合保持在一起,以便tableView的didSelectRowAtIndexPath能够使用类别对象。
我像这样加载我的表视图:
for (int i = 0; i < [items count]; i++){
head = [items objectAtIndex:indexPath.row];
labelName.text = head.DESC;
如何让我的NSPredicate搜索head.DESC的集合?
#pragma mark Search Results
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}
目前,我在运行上述代码时遇到此异常。
reason: 'Can't use in/contains operator with collection <Order: 0x7b22e900> (not a collection)'
答案 0 :(得分:0)
我想我理解你的问题。您希望能够通过items
属性过滤head
数组(这是一堆DESC
s)。如何使用块?
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject DESC] containsString:searchText];
}];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}
如果您的Order
对象将DESC
定义为@property
(或者是KVC兼容的),您将能够直接访问它。这应该有效:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self.DESC CONTAINS[cd] %@", searchText];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}
答案 1 :(得分:0)
我认为它可以帮到你:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"ANY self.DESC CONTAINS[cd] %@", searchText];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}