gpt4 book ai didi

ios - UITableView 中的 UISearchBar

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:52 24 4
gpt4 key购买 nike

我正在尝试将搜索功能添加到我的表格 View 中。我从创建 UITableView 开始,它工作得很好。问题是当我开始在搜索栏中书写时出现此错误:

Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<__NSCFString 0x8c44c00> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key name.'

然后我将搜索栏和搜索显示添加到 View Controller 。

searchdisplaycontroller socket :

enter image description here

View Controller socket :

enter image description here

在viewdidload中

self.filteredArray = [NSMutableArray arrayWithCapacity:[finalArray count]];

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. .." 应该可以解决问题。

可以看到the Apple docs on predicatesthis article获取更多信息。

关于ios - UITableView 中的 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com