简体   繁体   中英

UITableView dataSource must return cell from tableView:cellForRowAtIndexPath

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;

}

When you're using a UISearchDisplayController, the results are displayed in a separate UITableView that uses your original view controller as the delegate and data source. Since that UITableView isn't set up in your storyboard (it's dynamically created and loaded) it doesn't have access to your prototype cells. I was able to get a similar example to work by using:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

I'm a little concerned about that since you're asking one tableview to create cells to use in a different table view.

It seems like the better approach will be to not create your cells as prototype cells, but instead create them in and load them from, a separate nib file.

You should also read the documents for UISearchDisplayController as there are several other details of the dataSource and delegate protocols that need to be updated to reflect the fact that there are multiple tableviews in play.

This is telling you that your method: cellForRowAtIndexPath is returning a cell that is nil. Which generates this error.

This line here: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Will be nill, since you never assign anything to cell, nor do you do anything with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM