我需要一些帮助搜索/过滤核心数据实体。结果数组返回Null ..
我有一个带有搜索栏,控制器和tableview的根视图。此视图通常显示。
我正在调用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。我很感激任何建议。
答案 0 :(得分:1)
你忘了分配/初始化searchResults
数组。
- )