代码之家  ›  专栏  ›  技术社区  ›  Convolution

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

  •  0
  • Convolution  · 技术社区  · 15 年前

    我有一个带有搜索显示控制器的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;
    }
    

    如有任何帮助,我们将不胜感激,感谢您抽出时间来阅读本文。

    1 回复  |  直到 12 年前
        1
  •  0
  •   Convolution    15 年前

    奇怪的是,当在另一个开发者的手机上测试完全相同的代码时,程序并没有崩溃。答案不多,但让这对每个人都是一个教训,在多个设备上测试。

    推荐文章