2014-04-03 72 views
5

我在我的搜索栏中添加了一个问题,我在ViewController中添加了该问题。UITableView dataSource必须从tableView中返回单元格:cellForRowAtIndexPath

我收到以下错误:

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

谁能帮助?

回答

0

这是告诉你,你的方法:cellForRowAtIndexPath正在返回一个零的单元格。这会产生这个错误。

这条线的位置:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

将是nill,因为你永远不指定任何细胞,也不是你用它做任何事情。

+0

你假设他没有使用故事板指定的原型细胞(这是考虑到最安全的前提事实在手) –

+0

但当我点击搜索栏时弹出错误,正常的tableView内容显示在视图中 – user3450607

+0

我使用原型单元格 – user3450607

5

当您使用UISearchDisplayController时,结果显示在一个单独的UITableView中,该UITableView使用原始视图控制器作为委托和数据源。由于该UITableView没有在故事板中设置(它是动态创建和加载的),因此它无法访问您的原型单元。我能得到一个类似的例子,通过使用工作:

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

我有点担心,既然你问一个tableview中创建的细胞在不同的表视图中使用。

看来更好的方法是不创建单元格作为原型单元格,而是创建它们并从单独的nib文件加载它们。

您还应该阅读UISearchDisplayController的文档,因为还需要更新dataSource和委托协议的其他几个细节以反映有多个表视图在使用这一事实。

14

既然你已经添加了一个搜索栏,您需要检查,如果该细胞是不是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; 

} 
+0

这个答案适合我。谢谢! – Leandro

相关问题