2014-01-28 46 views
0

排序PFObjects數組我有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過濾器的工作,但...

+3

我想這不是'NSPredicate'錯誤,你能顯示你的'tableView:cellForRowAtIndexPath'方法嗎? –

+0

你沒有提供足夠的信息。您發佈了將數組過濾到結果數組中的代碼塊。這與你的表格視圖數據源有什麼關係?你需要提供更多關於你在做什麼的信息,特別是發佈你的cellForRowAtIndexPath方法,就像@Virussmca所說的那樣。 –

+0

@Virussmca編輯 – Apollo

回答

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