UITableView / UISearchBar 返回不正确的结果
我正在尝试在 UITableView 中实现搜索。搜索时,似乎返回了正确数量的结果,但我收到的条目来自结果中的原始故事数组,而不是 searchResults。我可以看到 searchResults 数组应该是数据源,但经过大量搜索后一直无法弄清楚如何使用 NSDictionaries 数组来实现它。任何帮助表示赞赏。
- (void)handleSearchForTerm:(NSString *)searchTerm {
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSDictionary *currentItem in [self stories])
{
if ([[currentItem objectForKey:@"title"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self searchResults] addObject:currentItem];
}
}
}
}
I am attempting to implement searching in a UITableView. When searching, it appears that the correct number of results are returned, but I am receiving entries from the original stories array in the results, rather than searchResults. I can see that the searchResults array should be the data source, but haven't been able to figure out after tons of searching quite how to pull it off with an array of NSDictionaries. Any help is appreciated.
- (void)handleSearchForTerm:(NSString *)searchTerm {
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSDictionary *currentItem in [self stories])
{
if ([[currentItem objectForKey:@"title"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self searchResults] addObject:currentItem];
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[tableView isEqual:self.searchDisplayController.searchResultsTableView]
也是创建和管理您自己的BOOL isFiltering;
变量的另一种替代方法[tableView isEqual:self.searchDisplayController.searchResultsTableView]
is also another alternative to making and managing your ownBOOL isFiltering;
variable使用 NSPredicate 进行过滤
假设您的原始数组是“originalArray”,因此要获取过滤后的数组,请使用它再创建两个全局变量
现在在搜索栏委托方法中执行以下操作
现在您需要更改我将位您的表视图委托和数据源, .... 对于您正在使用的所有地方,
请使用以下内容
use NSPredicate for filtering
Suppose that your original array is "originalArray" so to get the filtered array use this make two more global variables
Now in search bar delegate method do following
Now you need to change l'll bit your table view delegate and data source, .... for all the places where you are using
use following