uitableviewios4uisearchdisplaycontrollerios5uisearchresultscontroller

Display a custom background picture in UISearchResultsTableView in iOS


Currently I have this code that changes the default background in a TableView to a custom picture, but when the user does a search in the UISearchBar, the Search Results TableView is a plain white background. Where/how do I change it to be the same as my regular non-searched TableView?

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Employee List", @"Employee List");
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.clearsSelectionOnViewWillAppear = NO;
            self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
        }
        else {
            {
                UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Cloth.png"]];
                [tempImageView setFrame:self.tableView.frame]; 

                self.tableView.backgroundView = tempImageView;
            }
        }
    }
    return self;
}

Solution

  • You should be able to access the searchResults TableView through the searchDisplay Controller:

    ...
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Cloth.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    
    self.tableView.backgroundView = tempImageView;
    self.searchDisplayController.searchResultsTableView.backgroundView = tempImageView;
    ...