ios - 按用户名对 PFObject 数组进行排序

标签 ios objective-c parse-platform

我有一个 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/

相关文章:

ios - func tableView(tableView : UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

ios - 在 ViewController 之间传递 UIImage

ios - 如何在 UITextField (swift) 中启用清除按钮操作?

objective-c - 如何向 NSDictionary 添加 bool 值?

objective-c - Cocoa 合并自定义 View

ios - 使用 Parse 构建数据库

ios - 由于 keychainItemWrapper 导致临时 iOS 应用程序崩溃

objective-c - 计算网页加载到 UIWebView 所需的时间

ios - 从 PFFFacebookUtils 检索 Facebook 的用户 ID

Android服务在推送通知发送时启动并在服务任务完成后停止