簡體   English   中英

UITableView dataSource必須從tableView:cellForRowAtIndexPath返回單元格

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

我在我的ViewController添加了搜索欄的問題。

我收到以下錯誤:

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;

}

代碼搜索欄:

   - (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;
    }

有人可以幫忙嗎?

由於您添加了搜索欄,因此需要檢查單元格是否為零:

- (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;

}

當您使用UISearchDisplayController時,結果顯示在單獨的UITableView中,該視圖使用原始視圖控制器作為委托和數據源。 由於UITableView未在故事板中設置(它是動態創建和加載的),因此無法訪問原型單元格。 我能夠通過使用以下方式獲得類似的示例:

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

我有點擔心,因為你要求一個tableview創建在不同的表視圖中使用的單元格。

似乎更好的方法是不將您的單元格創建為原型單元格,而是創建它們並從單獨的nib文件加載它們。

您還應該閱讀UISearchDisplayController的文檔,因為還需要更新dataSource和委托協議的其他幾個細節,以反映有多個tableview正在運行的事實。

這告訴你你的方法: cellForRowAtIndexPath返回一個nil的單元格。 這會產生此錯誤。

這一行: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

將是nill,因為你從未向細胞分配任何東西,也沒有對它做任何事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM