我有一个带有搜索显示控制器的表格视图。它过去一直运行良好,但最近开始在某些搜索结果中崩溃。在这里,我的代码根据姓名、年龄和差点搜索高尔夫球手。数据已正确加载到表中,我可以访问并向下钻取以接收更多信息。但是,当我输入姓名或年龄的搜索查询时,应用程序崩溃,而 Golfers Handicap 则返回正常。
注意: dataSouceArray
是 tableview 的数据源,dataSourceArrayCopy
是用于在搜索过滤器中添加和删除对象的数据的可变副本。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
/*
Update the filtered array based on the search text and scope.
*/
[self.dataSourceArrayCopy 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.
*/
for (Golfer *golfer in dataSourceArray){
if ([scope isEqualToString:@"Name"] || [golfer.golferName isEqualToString:scope]){
NSComparisonResult result = [golfer.golferName compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.customerListCopy addObject:golfer];
}
}
if ([scope isEqualToString:@"Age"] || [golfer.golferAge isEqualToString:scope]){
NSComparisonResult result = [golfer.golferAge compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.dataSourceArrayCopy addObject:golfer];
}
}
if ([scope isEqualToString:@"Handicap"] || [golfer.golferHandicap isEqualToString:scope])
{
NSComparisonResult result = [golfer.golferHandicap compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.dataSourceArrayCopy addObject:golfer];
}
}
}
}
- (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.
return YES;
}
任何帮助将不胜感激,感谢您花时间阅读本文。