• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableView 中的 UISearchBar

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:32:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试向我的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap