我有一个带有搜索显示控制器的tableview。它在过去一直运作良好,但最近已开始崩溃某些搜索结果。在这里,我的代码根据他们的姓名,年龄和差点搜索高尔夫球手。数据已正确加载到表中,我可以访问和深入查看以获取更多信息。但是,当我输入名称或年龄的搜索查询时,应用程序崩溃,而高尔夫球手差点被罚款。
注意: 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;
}
任何帮助将不胜感激,感谢您花时间阅读本文。
答案 0 :(得分:0)
奇怪的是,当在另一个开发者手机上测试完全相同的代码时,它并没有使程序崩溃。答案不是很多,但让这成为每个人的一课,在多个设备上进行测试。