我有一个 PFUsers 数组,我正在尝试根据本地搜索结果过滤它们:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"username contains[cd] %@",
searchText];
_searchResults = [[_messages filteredArrayUsingPredicate:resultPredicate] mutableCopy];
NSLog(@"_searchResults: %@",_searchResults);
}
但这不起作用,最终会产生以下错误:
'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
有人知道我的 NSPredicate 有什么问题吗?谢谢!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"here?");
cell.textLabel.text = [_searchResults objectAtIndex:indexPath.row];
} else {
UILabel *name = (UILabel *)[cell viewWithTag:101];
if (_messages.count == 0)
name.text = @"No Messages";
else
name.text = @"name";
}
return cell;
}
我认为 NSPredicate 过滤器不起作用......
最佳答案
问题不在于您的 NSPredicate,而在于 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
未返回单元格这一事实。
如果“cell”= nil,此代码块将尝试使可重复使用的 cell 出队。在这种情况下,实际上从未创建新单元格,因此尝试将现有单元格出列将始终返回 nil。
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
相反,您需要检查是否存在可供重复使用的现有单元格,如果没有则创建一个新单元格。
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
关于ios - 按用户名对 PFObject 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396715/