簡體   English   中英

按用戶名排序PFObjects數組

[英]sorting an array of PFObjects by username

我有一個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沒有返回單元格。

如果“單元” = nil,則此代碼塊嘗試使可重用單元出隊。 在這種情況下,實際上不會創建新的單元,因此嘗試使現有單元出隊的操作將始終返回nil。

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

相反,您需要檢查是否有可重復使用的現有單元,如果沒有,請創建一個新單元。

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM