OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView 中的 UISearchBar [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:32
标题: ios - UITableView 中的 UISearchBar

我正在尝试向我的 tableview 添加搜索功能。我从创建 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 。

搜索显示 Controller 网点:

enter image description here

viewcontroller 网点:

enter image description here

在viewdidload中

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

numberOfRows 方法:

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"lol");
        return [filteredArray count];
    } else {
        return [rows count];
    }

}

其余方法:

-(void)filterContentForSearchTextNSString*)searchText scopeNSString*)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)searchDisplayControllerUISearchDisplayController *)controller shouldReloadTableForSearchStringNSString *)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)searchDisplayControllerUISearchDisplayController *)controller     shouldReloadTableForSearchScopeNSInteger)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;
}



Best Answer-推荐答案


问题在于您的谓词。如果您在错误消息中看到,它说 这个类不符合键名的键值编码。'

在你的谓词中,

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4