我在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;
}
任何人都可以帮忙吗?
答案 0 :(得分:14)
由于您添加了搜索栏,因此需要检查该单元格是否为零:
- (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;
}
答案 1 :(得分:5)
当您使用UISearchDisplayController时,结果显示在单独的UITableView中,该视图使用原始视图控制器作为委托和数据源。由于UITableView没有在故事板中设置(它是动态创建和加载的),因此它无法访问您的原型单元格。我能够通过使用以下方式获得类似的示例:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
我有点担心,因为您要求一个tableview创建要在不同表视图中使用的单元格。
似乎更好的方法是不将您的单元格创建为原型单元格,而是创建它们并从单独的nib文件加载它们。
您还应该阅读UISearchDisplayController的文档,因为还需要更新dataSource和委托协议的其他几个细节,以反映有多个表视图正在发挥作用的事实。
答案 2 :(得分:0)
这告诉您,您的方法:cellForRowAtIndexPath
正在返回一个nil的单元格。这会产生此错误。
这一行:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
将是nill,因为你从未向细胞分配任何东西,也不对它做任何事情。