所以这是代码
- (void)viewDidLoad {
[super viewDidLoad];
_queriedResults = [[NSMutableArray alloc]init]
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[_queriedResults addObjectsFromArray:[self searchBarQuery:[searchText lowercaseString]]];
}
-(NSArray*)searchBarQuery:(NSString*)searchedUser{
NSArray * users;
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:[searchedUser lowercaseString]];
users = [query findObjects];
return users;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [_queriedResults[indexPath.row]username];
return cell;
}
所以这些是我在视图控制器类中使用的主要功能。我已经做了一些调试,queriedResults确实成功地获取了我正在搜索的用户的名字,但是当我的tableView:cellForRowAtIndexPath使得应用程序崩溃时,queriedResults最终在其数组中有值。我设置了单元标识符,当我用普通的临时数组测试它时,代码工作正常。我不知道为什么当我从解析数据库中查询它时它会崩溃。
这是应用崩溃时收到的消息。
NSInternalInconsistencyException',原因:'无法使用标识符单元格对单元格进行出列 - 必须为标识符注册一个nib或类或在故事板中连接原型单元格“
我也收到了这个错误 -
断言失败 - [UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:],
但是我已经注册了单元格..这就是为什么它在我测试时与其他数组一起工作的原因。我不知道代码有什么问题。
答案 0 :(得分:2)
听起来您正在使用UISearchDisplayController或UISearchController。在这两种情况下,他们倾向于使用由控制器维护的 new 表视图重用cellForRow方法。一种解决方案是在这个新的表视图控制器上注册单元格,另一种解决方案是让原始表视图将单元格出列为新的表视图。
有关旧版UISearchDisplayController的更多信息,请参阅http://useyourloaf.com/blog/2012/09/06/search-bar-table-view-storyboard.html
有关较新的UISearchController的更多信息,请参阅http://useyourloaf.com/blog/2015/02/16/updating-to-the-ios-8-search-controller.html