Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UISearchBar to UITableView programmatically not working

Background: I have a UIViewController that, when loads, has a UITableView generated programmatically that gets its data from an SQLite3 database. This works fine.

Problem: I need to add a UISearchBar (and associated logic) but when I try, the UISearcBar is not rendered.

Code so far:

.h file:

#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "Exhibitor.h"
@interface ExhibitorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
    sqlite3 *congressDB;
    NSMutableArray *searchData;
    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}

@property (strong, nonatomic) IBOutlet UITableView *tableView;

-(NSString *) filePath;
-(void)openDB;

@end

.m file where the UISearchBar is added:

-(void)loadTableView
{
    CGRect usableSpace = [[UIScreen mainScreen] applicationFrame];
    CGFloat usableWidth = usableSpace.size.width;
    CGFloat usableHeight = usableSpace.size.height;

    UITableView *tableView = [[UITableView alloc] init];
    [tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; // I think this should have loaded the searchBar but doesn't

    // [self.tableView setTableHeaderView:searchBar]; // Have also tried this
    // [self.tableView.tableHeaderView addSubview:searchBar];  // And this
    NSLog(@"searchBar = %@", searchBar);  // This shows the searchBar is an object with values
    NSLog(@"HeaderView = %@", self.tableView.tableHeaderView);  // This shows the tableHeaderView as null ?? 

}

What am I doing wrong? What do I need to do to add a UISearchBar programmatically to a UITableView within a UIVewController?

like image 692
Agamemnon Avatar asked Dec 05 '22 07:12

Agamemnon


2 Answers

You should use a UITableViewController instead...

.h

@interface ExhibitorViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> {
    sqlite3 *congressDB;
    NSMutableArray *searchData;
    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;
}

-(NSString *) filePath;
-(void)openDB;

@end

.m

-(void)loadTableView {

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;

    self.tableView.tableHeaderView = searchBar; 

}

The problem is you were creating a table view but not assigning it to the property, and using UITableViewController makes things much simpler anyway...

If you want to keep it the way it is now, then you could just put self.tableView = tableVew; after [self.view addSubview:tableView];...

like image 163
jjv360 Avatar answered Dec 06 '22 21:12

jjv360


The problem is that

self.tableView

in loadTableView is nil or it is not that table that you have created programmatically.

Add

self.tableView = tableView;

after

[self.view addSubview:tableView];

Without this your searchBar is added to invalid tableView.

should

like image 30
Avt Avatar answered Dec 06 '22 21:12

Avt