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
?
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];
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With