按用户名排序对象数组

时间:2014-01-28 04:16:27

标签: 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过滤器不起作用......

1 个答案:

答案 0 :(得分:1)

问题不在于你的NSPredicate,而在于- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath没有返回一个单元格。

如果“cell”= nil,则此代码块会尝试使可重用单元出列。在这种情况下,永远不会创建新单元格,因此尝试使现有单元格出列将始终返回nil。

  if (cell == nil) {
   cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

相反,您需要检查是否有可用于重用的现有单元格,如果没有创建新单元格。

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}