Using a dynamic custom UITableViewCell in XCode 4.2, with Storyboards and UISeachDisplayController
Asked Answered
D

1

14

I've used Xcode 4.2 to create a storyboard-based iOS application. One of my screens contains a UITableViewController, using dynamic custom cells.

So far - so good.

Now, I wanted to add a UISearchDisplayController to allow filtering my list.

For some reason, the UISearchDisplayController won't display my custom cells, and I can't find a way to force it...

This is what my cellForRowAtIndexPath method looks:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"QueueListCell";
    QueueListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[QueueListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                             reuseIdentifier:CellIdentifier];
    }
    assert(cell);

    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
        indexPath = [_indexPathsForSearchResults objectAtIndex:indexPath.row];
    }

    // Set up the cell...
    NSDictionary* itemDict = [_ListItems objectAtIndex:indexPath.row];

    cell.labelQueueName.text = [itemDict objectForKey:kQueueName];
    cell.labelQueueNumItems.text = [[itemDict objectForKey:kQueueNumItems] stringValue];

    return cell;    
}

Any thoughts on how to get this working? I mean, my UISearchDisplayController table DOES show the correct number of results (I know that since I can click on them, and I added an NSLog to let me know what I'm clicking on...)

This is my table view This is my table view

This is how the search display table looks like... This is how the search display table looks like...

My problem/question is how to make the UISearchDisplayController table view show my custom cells?

Any help appreciated...

Reuven

Duckett answered 7/12, 2011 at 12:17 Comment(2)
You can always check the difference using the reference comparison of tableView.Brabble
note my problem is that for some reason the searchResultsTableView doesn't display my customer cells. That's what's I'm trying to solve (see images I added to the post)Duckett
B
27

Answer specific to query

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

For Complete example, have the sample code from apple's site.

Step by step illustration

Brabble answered 7/12, 2011 at 12:50 Comment(2)
Spark, you're my hero! :-) so, so, so frustrating. Yet, so so so simple and elegant to fix! THANKS!Duckett
That initially, I tried dequeuing a cell from the method parameter tableView (that knows nothing of my custom cells)... With @Spark's answer, I changed my code to dequeue the cell always from self.tableView - the table the knows about my custom cells...Duckett

© 2022 - 2024 — McMap. All rights reserved.