搜索显示控制器在返回结果时崩溃

问题描述:

我有一个带有搜索显示控制器的tableview。它过去一直很好,但最近已经开始崩溃某些搜索结果。在这里,我的代码根据他们的姓名,年龄和障碍搜索高尔夫球员。数据被正确加载到表格中,我可以访问和深入查看以获取更多信息。但是,当我输入名称或年龄的搜索查询时,应用程序崩溃,而高尔夫球手障碍返回正常。搜索显示控制器在返回结果时崩溃

注: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; 
} 

任何帮助将不胜感激,感谢您花时间阅读此。

+1

粗略一瞥,我没有看到任何明显的错误。您需要设置断点并逐步浏览代码,以查找崩溃并发布的确切行。 – TechZen 2010-03-18 15:09:26

+0

将来,请勿在代码中使用制表符来缩进格式。 – TechZen 2010-03-18 15:20:28

+0

谢谢你看看,我会稍后再报告。 – Convolution 2010-03-18 15:26:32

奇怪的是,当在另一个开发人员手机上测试完全相同的代码时,它并没有使程序崩溃。没有太多的答案,但让这成为每个人的教训,在多个设备上进行测试。