Ios UITableView中的UISearchBar
我正在尝试将搜索功能添加到我的tableview中。我从创建UITableView开始,它工作得非常完美。问题是,当我开始在搜索栏中写入时,出现以下错误:Ios UITableView中的UISearchBar,ios,objective-c,uitableview,uisearchbar,uisearchdisplaycontroller,Ios,Objective C,Uitableview,Uisearchbar,Uisearchdisplaycontroller,我正在尝试将搜索功能添加到我的tableview中。我从创建UITableView开始,它工作得非常完美。问题是,当我开始在搜索栏中写入时,出现以下错误: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x8c44c00> valueForUndefinedKey:]: this class is not key value coding-compl
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<__NSCFString 0x8c44c00> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key name.'
numberOfRows方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"lol");
return [filteredArray count];
} else {
return [rows count];
}
}
其余方法:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
filteredArray = [NSMutableArray arrayWithArray:[finalArray filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
问题在于你的谓词。如果您在错误消息中看到,它会说
此类不符合密钥名称的键值编码。
在你的谓词中
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
您正在搜索不存在的属性(name
)上的文本。在看不到更多代码或不了解更多您试图完成的任务的情况下,将谓词改为说@“SELF contains…”
,而不是说@“SELF.name contains…”
,应该可以实现这一目的
您可以查看或查看更多信息。帮助您在问题中添加屏幕截图。并投票给你5个荣誉,这样你下次就可以添加图片了。谢谢!我知道链接不是最优的,但这是我能找到的唯一解决方案。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];