ios - UITableView 中的 UISearchBar
<p><p>我正在尝试向我的 tableview 添加搜索功能。我从创建 UITableView 开始,它运行良好。问题是当我开始在搜索栏中写入时出现此错误:</p>
<pre><code>Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<__NSCFString 0x8c44c00> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key name.'
</code></pre>
<p>然后我将搜索栏和搜索显示添加到 ViewController 。 </p>
<p>搜索显示 Controller 网点:</p>
<p> <img src="/image/Voc3d.png" alt="enter image description here"/> </p>
<p>viewcontroller 网点:</p>
<p> <img src="/image/tmnH1.png" alt="enter image description here"/> </p>
<p>在viewdidload中</p>
<pre><code>self.filteredArray = ];
</code></pre>
<p>numberOfRows 方法:</p>
<pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"lol");
return ;
} else {
return ;
}
}
</code></pre>
<p>其余方法:</p>
<pre><code>-(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
;
// Filter the array using NSPredicate
NSPredicate *predicate = %@",searchText];
filteredArray = ];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[ objectAtIndex:]];
// 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:
[ objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题在于您的谓词。如果您在错误消息中看到,它说 <code>这个类不符合键名的键值编码。'</code></p>
<p>在你的谓词中,</p>
<pre><code>NSPredicate *predicate = %@",searchText];
</code></pre>
<p>您正在搜索不存在的属性上的文本 (<code>name</code>)。在没有看到更多代码或不知道更多您想要完成的内容的情况下,将谓词更改为 <code>@"SELF contains..."</code> 而不是 <code>@"SELF.name contains。 .."</code> 应该可以解决问题。</p>
<p>你可以看到<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/predicates.html#//apple_ref/doc/uid/TP40001798-SW1" rel="noreferrer noopener nofollow">the Apple docs on predicates</a>或 <a href="http://useyourloaf.com/blog/2010/10/19/searching-arrays-with-nspredicate-and-blocks.html" rel="noreferrer noopener nofollow">this article</a>了解更多信息。</p></p>
<p style="font-size: 20px;">关于ios - UITableView 中的 UISearchBar,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21564334/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21564334/
</a>
</p>
页:
[1]