具有搜索功能的表视图出错
Error on tableview with search functionality
我有一个以编程方式创建搜索功能的视图。该应用程序在 iOS 6 中运行良好,但在设备 运行 iOS 7 上崩溃。我收到一条错误消息“由于未捕获的异常正在终止应用程序 'NSInvalidArgumentException',原因:'A nil string was passed for sorting.'
*** 首先抛出调用堆栈:“。我做错了什么吗?这是在 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;
}
当按 UILocalizedIndexedCollation
排序的集合有多个对象时,当对它们调用选择器时返回 nil
时,我遇到了这个问题。
在你的代码中,这是对我来说中断的行:
NSArray *sortedSection = [indexedCollation sortedArrayFromArray:singleSectionArray collationStringSelector:@selector(sProductSearch)];
我的解决方案是确保在调用选择器 时返回 nil
的任何对象没有被添加到发送到 [=12] 的数组中 =].
例如,在你的代码中,我做了这样的事情:
for (Product *theRetailer in productsWithNoBarcodeArray)
{
if ([theRetailer valueForKey:@"sProductSearch"] != nil)
{
[(NSMutableArray *)[sectionsArray objectAtIndex:theRetailer.section] addObject:theRetailer];
}
}
我有一个以编程方式创建搜索功能的视图。该应用程序在 iOS 6 中运行良好,但在设备 运行 iOS 7 上崩溃。我收到一条错误消息“由于未捕获的异常正在终止应用程序 'NSInvalidArgumentException',原因:'A nil string was passed for sorting.' *** 首先抛出调用堆栈:“。我做错了什么吗?这是在 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;
}
当按 UILocalizedIndexedCollation
排序的集合有多个对象时,当对它们调用选择器时返回 nil
时,我遇到了这个问题。
在你的代码中,这是对我来说中断的行:
NSArray *sortedSection = [indexedCollation sortedArrayFromArray:singleSectionArray collationStringSelector:@selector(sProductSearch)];
我的解决方案是确保在调用选择器 时返回 nil
的任何对象没有被添加到发送到 [=12] 的数组中 =].
例如,在你的代码中,我做了这样的事情:
for (Product *theRetailer in productsWithNoBarcodeArray)
{
if ([theRetailer valueForKey:@"sProductSearch"] != nil)
{
[(NSMutableArray *)[sectionsArray objectAtIndex:theRetailer.section] addObject:theRetailer];
}
}