Reputation: 5645
I have a search bar embedded to UINavigationBar
in my iOS application. I have a UIViewController
. When user taps search bar I need to toggle UITableView to show searched content. In background I have a map. It is just like in iPhone's Maps application. Any ideas?
Upvotes: 0
Views: 150
Reputation: 255
You could add a tableview in front of the map view. The table view will be hidden by default while the map is shown. When the user searches, you can unhide the table view and refresh the results. If user cancels, then you would hide the table view again.
Add the table view as a subview of the map view:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.mapView.frame.size.width, self.mapView.frame.size.height) style:UITableViewStylePlain];
self.tableView.hidden = YES;
[self.mapView addSubview:self.tableView];
}
Use the following UISearchBarDelegate method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.resultsArray removeAllObjects];
self.tableView.hidden = NO;
// After grabbing the results, add to array
// Then reload the table view
[self.tableView reloadData];
}
When user taps the cancel button:
- (void)cancelButtonTapped:(id)sender {
self.tableView.hidden = YES;
}
For the keyboard, Apple Maps hides the keyboard upon scrolling the table view. You can use the UIScrollViewDelegate (since UITableView is a subclass of UIScrollView) to achieve this:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[searchBar resignFirstResponder];
}
Upvotes: 1