2015-01-19 2 views
0

У меня есть вид, где я программно создаю функцию поиска. Приложение отлично работает в iOS 6, но выходит из строя на устройствах под управлением iOS 7. Я получаю сообщение об ошибке «Завершение приложения из-за неперехваченного исключения« NSInvalidArgumentException », причина:« Для сортировки была отправлена ​​нулевая строка. ' *** Первый бросить стек вызовов: «Есть ли что-то не так, что я делаю? Вот код, который выполняется в методе viewDidLoad:Ошибка в представлении таблицы с функцией поиска

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    { 
     // Get the DBAccess object; 
     DBAccess *dbAccess = [[DBAccess alloc] init]; 

     // Get array of products that do not have a barcode 
     productsWithNoBarcodeArray = [dbAccess getUserProductsWithNoBarcode]; 


     // Close the database because we are finished with it 
     [dbAccess closeDatabase]; 

    } 

    NSLog(@"Number of products with no barcodes is: %d",[productsWithNoBarcodeArray count]); 


    /***************************************Implementing Tableview Sections and Index*****************************/ 

    self.retailers = [NSMutableArray arrayWithCapacity:1]; 

    UILocalizedIndexedCollation *indexedCollation = [UILocalizedIndexedCollation currentCollation]; 

    //Iterate over the retailers, populating their section number 

    for (Product *theRetailer in productsWithNoBarcodeArray) 
    { 
     NSInteger section = [indexedCollation sectionForObject:theRetailer collationStringSelector:@selector(sProductSearch)]; 

     theRetailer.section = section; 

    } 

    //Get the count of the number of sections 

    NSInteger sectionCount = [[indexedCollation sectionTitles] count]; 

    //Create an array to hold subarrays for the various sections 

    NSMutableArray *sectionsArray = [NSMutableArray arrayWithCapacity:sectionCount]; 

    //Iterate over each section, creating each subarray 

    for (int i=0; i<= sectionCount; i++) 
    { 
     NSMutableArray *singleSectionArray = [NSMutableArray arrayWithCapacity:1]; 

     [sectionsArray addObject:singleSectionArray]; 
    } 

    //Iterate over the retailers, putting each retailer into the correct subarray 

    for (Product *theRetailer in productsWithNoBarcodeArray) 
    { 
     [(NSMutableArray *)[sectionsArray objectAtIndex:theRetailer.section] addObject:theRetailer]; 

    } 

    //Iterate over each section array to sort items in the section 

    for (NSMutableArray *singleSectionArray in sectionsArray) 
    { 
     //Use the UILocalizedIndexedCollation sortedArrayFromArray: method to sort each array 

     NSArray *sortedSection = [indexedCollation sortedArrayFromArray:singleSectionArray collationStringSelector:@selector(sProductSearch)]; 


     NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"sItemDescription" ascending:NO]; // 1 
     NSArray *sortedByNameArray = [sortedSection sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]; 

     // [self.retailers addObject:sortedSection]; 
     [self.retailers addObject:sortedByNameArray]; 

    } 


    NSLog(@"The count on manualTap retailers is: %d",[self.retailers count]); 

    /***************************************END - Implementing Tableview Sections and Index*****************************/ 



    /*********************Programmatically create a search bar***********************/ 

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320.0f, 44.0f)]; 

    self.shoppingListTable.tableHeaderView = self.searchBar; 

    //Create and configure the search controller 

    self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]; 

    self.searchController.searchResultsDataSource = self; 
    self.searchController.searchResultsDelegate = self; 

    bfiltered = NO; 

    searchBar.keyboardType = UIKeyboardTypeNumbersAndPunctuation; //UIKeyboardTypeNumberPad; 




} 

ответ

1

я случайно в это, когда коллекция, которая сортируется по UILocalizedIndexedCollation было несколько объектов, возвращаемых nil, когда селектор был вызван на них.

В своем коде, это была линия, которая ломится для меня:

NSArray *sortedSection = [indexedCollation sortedArrayFromArray:singleSectionArray collationStringSelector:@selector(sProductSearch)]; 

Мое решение было убедиться, что все объекты, которые возвращаются nil, когда селектор был вызван не добавляются в массив, отправленный на UILocalizedIndexedCollation.

Например, в коде, я сделал что-то вроде этого:

for (Product *theRetailer in productsWithNoBarcodeArray) 
{ 
    if ([theRetailer valueForKey:@"sProductSearch"] != nil) 
    { 
     [(NSMutableArray *)[sectionsArray objectAtIndex:theRetailer.section] addObject:theRetailer]; 
    } 
} 
+0

100% ..... объекты возвращавшихся ноль. Сортировано сейчас, спасибо. – BDR

Смежные вопросы