検索ディスプレイコントローラーを備えたテーブルビューがあります。以前は正常に機能していましたが、最近、特定の検索結果でクラッシュし始めました。ここで私のコードは、名前、年齢、ハンディキャップに基づいてゴルファーを検索します。データはテーブルに正しくロードされています。アクセスしてドリルダウンすると、さらに情報を受け取ることができます。ただし、名前または年齢の検索クエリを入力すると、アプリがクラッシュし、ゴルファーのハンディキャップは正常に返されます。
注: dataSouceArray
はテーブルビューのデータソースであり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;
}
どんな助けでもありがたいです、これを読むために時間を割いてくれてありがとう。