コア データ エンティティの検索/フィルタリングに助けが必要です。結果配列は Null を返します。
検索バー、コントローラー、およびテーブルビューを備えたルート ビューがあります。このビューは正常に表示されます。
UISearchBarDelegate と UISearchDisplayDelegate を呼び出しています。
可変配列 (searchResults) があります。
私の検索コードは次のとおりです。
-(void) filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSLog(@"%s", __FUNCTION__);
[self.searchResults removeAllObjects];
for (Entity *ent in [self.fetchedResultsController fetchedObjects])
{
if ([scope isEqualToString:@"All"] || [ent.title isEqualToString:scope])
{
NSRange range = [ent.title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
NSLog(@"Adding title '%@' to searchResults as it contains '%@'", ent.title, searchText);
[self.searchResults addObject:ent];
}
}
}
NSLog(@"The searchResults array contains '%@'", searchResults); <<<< RETURNS NULL
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:@"All"];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text ]scope:@"All"];
return YES;
}
セル構成コードは次のとおりです。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyVeryOwnCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Entity *entity = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//NSLog(@"Configuring cell to show search results");
entity = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
//NSLog(@"Configuring cell to show normal data");
entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
cell.textLabel.text = entity.title;
return cell;
}
searchResults 配列が null のように見えるので、何かばかげたことをしているに違いありません。アドバイスをいただければ幸いです。