DanielR
DanielR

Reputation: 711

UIViewController with UITableViews and UISearchBar

previously I had a UITableViewController with a UISearchBar and it was showing up at the top by using self.tableView.tableHeaderView = searchBar;

Now I had to redo some things and ended up with a UIViewController which shows 2 UITableView's. I wanted the upper one to show the search bar but now it's not being shown.

Now I'm using [MyView setTableHeaderView:searchBar]; in viewDidLoad Method to add the searchBar to the TableView.

In the UIViewController the code is almost the same as in the UITableView (searchbar initialized and frame created as well as tableview initialized)

Any ideas what I'm doing wrong?

EDIT: Idea behind all this to have a list in the upper tableview (including search bar) and another uitableview where I can choose some filtering options.

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
    searchResult = [[NSMutableArray alloc] init];

    LexikonView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 337) style:UITableViewStylePlain];
    LexikonView.dataSource = self;
    LexikonView.delegate = self;
    [self.view addSubview:LexikonView];

    FilterView = [[UITableView alloc] initWithFrame:CGRectMake(0, 381, 320, 0) style:UITableViewStylePlain];
    //FilterView.dataSource = self;
    //FilterView.delegate = self;
    [self.view addSubview:FilterView];
}
return self;
}

(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.autocorrectionType = UITextAutocorrectionTypeYes;

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;

[LexikonView setTableHeaderView:searchBar];
}

For now I just want the searchbar to work again (as it did using a uitableviewcontroller). If I reduce the size of the upper tableview I only see empty space. I added a UISearchBar in IB and voilá there it is, but the delegate methods are not called.
And yes I want the searchbar to be always visible at the top of the tableview

Upvotes: 0

Views: 2762

Answers (1)

LJ Wilson
LJ Wilson

Reputation: 14427

I think you want a TableView with a SearchBar that is always visible right? If this is the case, you just need to add a TableView to a ViewController and leave room at the top for the SearchBar. Add the SearchBar independently of the TableView. I have a post that shows how this is done and also gives you some direction for really fast searching of the backing data for the TableView.

If this isn't what you are after, please be a little more specific in what your goals are here. Also please post the code you are using to try to accomplish what you are trying to do.

Upvotes: 2

Related Questions