基本上我正在尝试在我的iPhone应用程序中添加搜索功能,但目前我没有太多运气。我已经下载了Apple提供的Table Search应用程序,并已将代码复制到我的应用程序中。它构建正常,但这是问题所在。与Apple示例不同,我的所有数据都存储在一个数组中,可以通过调用[ciParser.currentArray]来访问。
有关如何更改代码以满足我的需求的任何想法?我收到错误“由于未捕获的异常'终止应用'NSRangeException',原因:'*** - [NSCFArray objectAtIndex:]:索引(0)超出边界(0)'”每当我点击搜索栏和应用程序退出。下面是调试器突出显示麻烦的代码。
显然这个错误意味着尝试搜索的数据库是空的,但我可能是错的。
仅供参考我的应用程序使用ciParser引用的类来下载和解析RSS提要 - 而后者又将下载的内容存储在我需要搜索的数组中。
更新:现在看来我的表在找到信息时没有更新。这是我应该做的代码。有任何想法吗?感谢。
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[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
{
[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.
解析代码:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
CurrentItem * nextCurrentItem=[ciParser.currentArray objectAtIndex:indexPath.row];
if (tableView == self.searchDisplayController.searchResultsTableView)
{
// **(Code which debugger points to as being wrong)**
nextCurrentItem = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
nextCurrentItem = [ciParser.currentArray objectAtIndex:indexPath.row];
}
NSString*settingValue = [[NSUserDefaults standardUserDefaults]stringForKey:@"state"];
if ([settingValue isEqualToString:@"New South Wales"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"Western Australia"]) {
cell.textLabel.text=@"The FESA does not provide any Current Incident reports.";
}
else if ([settingValue isEqualToString:@"Victoria"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"South Australia"]) {
cell.textLabel.text=nextCurrentItem.title;
}
else if ([settingValue isEqualToString:@"Tasmania"]) {
cell.textLabel.text=nextCurrentItem.title;
}
// Set up the cell...
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
更新:这是我认为不对的部分,因为我的搜索结果现在显示为空白,无论我输入什么内容。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
//**This part threw many errors and I eventually got it to this but I suspect this is my problem.**
for (currentItem in ciParser.currentArray)
{
if ([scope isEqualToString:@"All"] || [currentItem.title isEqualToString:scope])
{
NSComparisonResult result = [currentItem.title compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:currentItem];
}
}
}
}
答案 0 :(得分:1)
您需要在filterContentForSearchText:scope:
中设置断点/日志,以查看出错的地方。
NSLog(@"searchText=%@",searchText);
NSLog(@"scope=%@",scope);
for (currentItem in ciParser.currentArray)
{
NSLog(@"currentItem=%@",currentItem);
if ([scope isEqualToString:@"All"] || [currentItem.title isEqualToString:scope])
{
NSLog(@"in ([scope isEqualToString:@"All"] || [currentItem.title isEqualToString:scope])");
NSComparisonResult result = [currentItem.title compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
NSLog(@"result=%d",result);
if (result == NSOrderedSame)
{
NSLog(@"in (result == NSOrderedSame)");
[self.filteredListContent addObject:currentItem];
NSLog(@"self.filteredListContent=%@",self.filteredListContent);
}
}
}
这将为您提供操作方法的完整快照,并告诉您代码出错的位置。
答案 1 :(得分:0)
您是否更新了“numberOfRowsInSection”的代码,以便在搜索时返回已过滤的项目数?如果没有,这听起来像你的问题。
答案 2 :(得分:0)
您需要实施:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
还有:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
我建议您首先关注搜索字符串或搜索选项。甚至尝试实现一个只将每个项目复制到过滤后的数组并检查它是否有效的函数。