0

I have an array of PFUsers and I'm trying to filter them based on local search results:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"username contains[cd] %@",
                                    searchText];

    _searchResults = [[_messages filteredArrayUsingPredicate:resultPredicate] mutableCopy];
    NSLog(@"_searchResults: %@",_searchResults);
}

But this doesn't work and ends up producing the following error:

'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Does anybody know what's wrong with my NSPredicate? Thank you!

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

I don't think the NSPredicate filter is working though...

5
  • 3
    I guess it is not NSPredicate error, Can you show your tableView:cellForRowAtIndexPath method? Commented Jan 28, 2014 at 4:19
  • You are not providing nearly enough information. You posted a block of code that filters an array into a results array. How does that relate to your table view data source? You need to provide a lot more information about what you're doing, and especially, post your cellForRowAtIndexPath method, as @Virussmca says. Commented Jan 28, 2014 at 4:26
  • try to NSlog the _searchResults array in the cellForRowAtIndexPath method and use breakpoints to prevent app from crashing. Commented Jan 28, 2014 at 11:39
  • @GoGreen I am doing that and the _search results array never contains anything... Commented Jan 28, 2014 at 14:38
  • what about the same _searchResults array in filterContentForSearchText:scope: ? does it contain any value there? If no, then are you sure you have a user with the same character sequence as the searchText ? Commented Jan 28, 2014 at 14:56

1 Answer 1

1

The issue is not with your NSPredicate but rather the fact that - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not returning a cell.

This block of code attempts to dequeue a reusable cell if the "cell" = nil. In this case a new cell is never actually created so attempting to dequeue an existing cell will always return nil.

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

Instead you need to check if there is an existing cell available for reuse and if not create a new one.

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.