【问题标题】:xCode search bar crash when I'm trying to query data with parse当我尝试使用解析查询数据时,xCode 搜索栏崩溃
【发布时间】:2015-07-22 16:36:52
【问题描述】:

这是代码

- (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 确实成功地获取了我正在搜索的用户的名称,但是当 queriedResults 最终在其数组中包含值时,我的 tableView:cellForRowAtIndexPath 使应用程序崩溃。我已经设置了单元格标识符,并且当我使用普通的临时数组对其进行测试时,代码工作正常。我不知道为什么从解析数据库中查询它时它会崩溃。

这是我在应用崩溃时收到的消息。

NSInternalInconsistencyException', reason: '无法使用标识符单元格出列单元格 - 必须为标识符注册一个 nib 或类或连接情节提要中的原型单元格'

我也遇到了这个错误 -

-[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:] 中的断言失败,

但是我想我已经注册了单元格。这就是为什么在我测试时它可以与其他数组一起使用。我不知道代码有什么问题。

【问题讨论】:

  • _queriedResults 是您在测试时唯一更改的内容?你在使用搜索结果控制器吗?
  • 肯定不是 Xcode 的搜索栏崩溃了吧?
  • 我正在使用搜索栏和搜索显示控制器,我也收到此错误 - 断言失败 -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:],

标签: ios objective-c searchbar


【解决方案1】:

听起来您正在使用 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

【讨论】:

  • 我只需要弄清楚为什么我的表视图没有正确显示我从数据库查询得到的数组但崩溃了,但是当它只是一个普通数组时它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 2010-12-16
  • 1970-01-01
相关资源
最近更新 更多