带有搜索功能的tableview出错

时间:2015-01-19 19:57:47

标签: uitableview searchbar

我有一个视图,我以编程方式创建搜索功能。该应用程序在iOS 6中正常运行,但在运行iOS 7的设备上崩溃。我收到一条错误,指出“因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'传递了一个nil字符串进行排序。' ***首先抛出调用堆栈:“。我有什么问题吗?这是在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 个答案:

答案 0 :(得分: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];
    }
}