简体   繁体   English

UITableView dataSource必须从tableView:cellForRowAtIndexPath返回单元格

[英]UITableView dataSource must return cell from tableView:cellForRowAtIndexPath

I have a problem with my search bar I added in my ViewController . 我在我的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 : 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. 当您使用UISearchDisplayController时,结果显示在单独的UITableView中,该视图使用原始视图控制器作为委托和数据源。 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. 由于UITableView未在故事板中设置(它是动态创建和加载的),因此无法访问原型单元格。 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. 我有点担心,因为你要求一个tableview创建在不同的表视图中使用的单元格。

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. 似乎更好的方法是不将您的单元格创建为原型单元格,而是创建它们并从单独的nib文件加载它们。

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. 您还应该阅读UISearchDisplayController的文档,因为还需要更新dataSource和委托协议的其他几个细节,以反映有多个tableview正在运行的事实。

This is telling you that your method: cellForRowAtIndexPath is returning a cell that is nil. 这告诉你你的方法: cellForRowAtIndexPath返回一个nil的单元格。 Which generates this error. 这会产生此错误。

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

Will be nill, since you never assign anything to cell, nor do you do anything with it. 将是nill,因为你从未向细胞分配任何东西,也没有对它做任何事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableView数据源必须从tableView:cellForRowAtIndexPath错误返回单元格 - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath eror UITableView数据源必须从tableView:cellForRowAtIndexPath:返回一个单元格: - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:? iOS:UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格: - iOS: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: UITableView数据源必须从tableView:cellForRowAtIndexPath返回单元格” - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath" UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格 - UITableView dataSource must return a cell from tableView: cellForRowAtIndexPath UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格:但我确实 - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: but I do 原因:“ UITableView数据源必须从tableView:cellForRowAtIndexPath返回单元格 - reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath 由于UITableView dataSource必须在iOS 5.1 Simulator中从tableView:cellForRowAtIndexPath:返回单元格而导致应用程序崩溃 - app crash due to UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: in iOS 5.1 Simulator 我的应用崩溃,原因是'UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格 - My app crash, reason 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath Xcode数据源必须从tableView:cellForRowAtIndexPath返回一个单元格 - Xcode dataSource must return a cell from tableView:cellForRowAtIndexPath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM