Jonathan Swinney
Jonathan Swinney

Reputation: 1094

iOS 5 Search Display Controller and reuse of UITableViewController

The background

I have an iPhone app that makes use of a UITableViewController sub class in several places by sub classing again for each use. One of those uses is a search controller.

@interface TableViewController : UITableViewController
// ...
@interface SearchTableViewController : TableViewController <UISearchDisplayDelegate, UISearchBarDelegate>

In the storyboard editor I have the same table view, cell structure, and reuse identifier in each view that makes use of TableViewController. Everywhere I am using it the storyboard is generating cells for me based on my design time prototypes so that in the tableView:cellForRowAtIndexPath: method I don't have to include the if (cell == nil) section.

The problem

I have done something wrong and my search controller's cells aren't being generated by the storyboard like the others. At first, I added in the if (cell == nil) bit to solve the problem but it causes my search to display blank rows. Actually, the search shows the correct number of blank rows. After you cancel the search, the results appear from out of the background. Here's the code from TableViewController:

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

    ModelObject* obj = [self.dataSource modelAtIndex:indexPath.row];
    UILabel* name = (UILabel*) [cell viewWithTag:1];
    name.text = obj.name;
    return cell;
}

I suspect there may be other details necessary to identify the issue but any tips on what kinds of things could cause this would be helpful. Thanks.

Upvotes: 2

Views: 1069

Answers (2)

rixian
rixian

Reputation: 41

The part of "tableview" is wrong.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

you should use self.tableview (or the IBOutlet variable name you linked to your tableview) instead. Like this;

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

The tableview param in cellForRowAtIndexPath will not be the one you have in your viewcontroller when search is being done. That is the reason of the issue.

Upvotes: 1

Weel
Weel

Reputation: 132

if you are using Storyboard, select the object "Search bar with search display controller" and place it in your view controller (let's say VC). Then Xcode will automatically link search bar to a search display controller, which resides in VC. Therefore in VC, you can access search display controller by self.searchDisplayController and your VC should adopt protocols such as UISearchBarDelegate, UISearchDisplayDelegate Also your search bar can be accessed by self.searchDisplayController.searchBar

Implement relative methods (table view etc) in the UISearchDisplayDelegate protocol. Bingo! Your search result will be displayed automatically.

Upvotes: 1

Related Questions