gpt4 book ai didi

ios - 当我尝试使用解析查询数据时,xCode 搜索栏崩溃

转载 作者:行者123 更新时间:2023-11-28 21:42:14 25 4
gpt4 key购买 nike

代码如下

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

这些是我在 View Controller 类中使用的主要函数。我已经完成了一些调试,queriedResults 确实成功地获取了我正在搜索的用户的名称,但是当 queriedResults 最终在其数组中包含值时,我的 tableView:cellForRowAtIndexPath 使应用程序崩溃。我已经设置了单元格标识符,当我使用普通临时数组对其进行测试时代码工作正常。我不知道为什么当我从解析数据库中查询它时它会崩溃。

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

NSInternalInconsistencyException',原因:'无法使具有标识符 cell 的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者连接 Storyboard中的原型(prototype)单元格'

我也遇到这个错误 -

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

但我认为我已经注册了单元格.. 这就是为什么它在我测试时与其他阵列一起工作的原因。我不知道代码有什么问题。

最佳答案

听起来您正在使用 UISearchDisplayController 或 UISearchController。在这两种情况下,他们都倾向于将 cellForRow 方法与由 Controller 维护的 TableView 重用。一种解决方案是在这个新的 TableView Controller 上注册单元格,另一种解决方案是要求原始 TableView 为新 TableView 将单元格出队。

有关旧版 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

关于ios - 当我尝试使用解析查询数据时,xCode 搜索栏崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569040/

25 4 0