BDR
BDR

Reputation: 143

Error on tableview with search functionality

I have a view where I programmatically create Search functionality. The App works fine in iOS 6 but crashes on devices running iOS 7. I get an error stating "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'A nil string was passed for sorting.' *** First throw call stack:".Is there anything wrong that I'm doing? Here's the code that executes in the viewDidLoad method:

- (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;




}

Upvotes: 0

Views: 244

Answers (1)

Mike Burba
Mike Burba

Reputation: 1025

I happened into this when the collection that is being sorted by the UILocalizedIndexedCollation had multiple objects that returned nil when the selector was invoked on them.

In your code, this was the line that was breaking for me:

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

My solution was to make sure that any objects that returned nil when the selector was invoked didn't get added to the array sent to the UILocalizedIndexedCollation.

For example, in your code, I did something like this:

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

Upvotes: 1

Related Questions