I have a problem with my search bar I added in my ViewController
.
I am getting the following error:
Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:6509
2014-04-03 18:08:24.355 MyFood[6405:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
configureCell
:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// ToDos *toDo = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (viewMode==AlphabeticalOrder) {
Inventory *toDo=nil;
if (self.searchDisplayController.isActive) {
toDo = [searchResults objectAtIndex:indexPath.row];
} else {
toDo=[inventoryProducts objectAtIndex:indexPath.row];
}
cell.textLabel.text = toDo.inventoryProductName;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSDate *dt = toDo.expireDate;
NSString *dateAsString = [formatter stringFromDate:dt];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Until: %@", dateAsString];
} else {
Inventory *toDo=nil;
if (self.searchDisplayController.isActive) {
NSDictionary *sectionDict=[searchResults objectAtIndex:indexPath.section];
NSArray *sectionData=[sectionDict objectForKey:@"SECTION_DATA"];
toDo = [sectionData objectAtIndex:indexPath.row];
} else {
NSDictionary *sectionDict=[inventoryProducts objectAtIndex:indexPath.section];
NSArray *sectionData=[sectionDict objectForKey:@"SECTION_DATA"];
toDo=[sectionData objectAtIndex:indexPath.row];
}
cell.textLabel.text = toDo.inventoryProductName;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSDate *dt = toDo.expireDate;
NSString *dateAsString = [formatter stringFromDate:dt];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Until: %@", dateAsString];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
code search bar:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
if (searchText.length!=0) {
if (viewMode==AlphabeticalOrder) {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
// [searchResults removeAllObjects];
// [searchResults addObjectsFromArray:[inventoryProducts filteredArrayUsingPredicate:resultPredicate]];
searchResults=[inventoryProducts filteredArrayUsingPredicate:resultPredicate];
} else {
//-section mode
NSMutableArray *newDataArray=[NSMutableArray array];
for (NSMutableDictionary *aSectionDict in inventoryProducts) {
NSArray *sectionData=(NSArray*)[aSectionDict objectForKey:@"SECTION_DATA"];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
NSArray *filteredArray=[sectionData filteredArrayUsingPredicate:resultPredicate];
NSDictionary *sectionDataModified=[NSDictionary dictionaryWithObjectsAndKeys:filteredArray,@"SECTION_DATA",[aSectionDict objectForKey:@"SECTION_TITLE"],@"SECTION_TITLE", nil];
[newDataArray addObject:sectionDataModified];
}
searchResults=[NSArray arrayWithArray:newDataArray];
}
}
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
can anyone help?
Since you have added a search bar, you need to check if the cell is not nil:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// This is added for a Search Bar - otherwise it will crash due to
//'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With