我一直在尝试将简单的搜索功能添加到我的应用程序的TableViewController中。我遵循了雷·温德利希的教程。我有一个包含一些数据的tableView,我在storyboard中添加了搜索栏+显示控制器,然后我有了以下代码:
#pragma mark - Table View
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BreedCell" forIndexPath:indexPath];
//Create PetBreed Object and return corresponding breed from corresponding array
PetBreed *petBreed = nil;
if(tableView == self.searchDisplayController.searchResultsTableView)
petBreed = [_filteredBreedsArray objectAtIndex:indexPath.row];
else
petBreed = [_breedsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = petBreed.name;
return cell;
}
#pragma mark - Search
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[_filteredBreedsArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchString];
_filteredBreedsArray = [[_breedsArray filteredArrayUsingPredicate:predicate] mutableCopy];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[_filteredBreedsArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",self.searchDisplayController.searchBar.text];
_filteredBreedsArray = [[_breedsArray filteredArrayUsingPredicate:predicate] mutableCopy];
return YES;
}
这是标准的东西,但是当我在搜索栏中输入文本时,它每次都会崩溃,并显示以下错误:
2013-01-07 19:47:07.330 FindFeedo[3206:c07] *** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460
2013-01-07 19:47:07.330 FindFeedo[3206:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier BreedCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我知道在iOS 6中,信元的处理和出列系统发生了变化,而且搜索使用了不同的tableView,所以我认为问题是带有过滤结果的搜索tableView不知道该信元,所以我在我的viewDidLoad中放入了以下内容:
[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"BreedCell"];
瞧!它成功了..。只有在你第一次搜索的时候。如果您返回到原始结果并再次搜索,应用程序崩溃并返回相同的错误。我想也许可以添加所有的
if(!cell){//init cell here};
填充到cellForRow方法中,但这是否与拥有dequeueReusableCellWithIdentifier:forIndexPath:方法的全部目的背道而驰呢?不管怎么说,我迷路了。我遗漏了什么?请帮帮忙。提前感谢您的宝贵时间(:
亚历克斯。
https://stackoverflow.com/questions/14207142
复制相似问题