UISearchBar - 切换 UITableView
UISearchBar - toggle UITableView
我的 iOS 应用程序中有一个嵌入到 UINavigationBar
的搜索栏。我有一个UIViewController
。当用户点击搜索栏时,我需要切换 UITableView 以显示搜索到的内容。在背景中,我有一张地图。就像 iPhone 的地图应用程序一样。有什么想法吗?
您可以在地图视图前面添加一个 table视图。显示地图时,table 视图将默认隐藏。当用户搜索时,您可以取消隐藏 table 视图并刷新结果。如果用户取消,那么您将再次隐藏 table 视图。
添加 table 视图作为地图视图的子视图:
- (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];
}
使用以下 UISearchBarDelegate 方法:
- (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];
}
当用户点击取消按钮时:
- (void)cancelButtonTapped:(id)sender {
self.tableView.hidden = YES;
}
对于键盘,Apple 地图会在滚动 table 视图时隐藏键盘。您可以使用 UIScrollViewDelegate(因为 UITableView 是 UIScrollView 的子类)来实现此目的:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[searchBar resignFirstResponder];
}
我的 iOS 应用程序中有一个嵌入到 UINavigationBar
的搜索栏。我有一个UIViewController
。当用户点击搜索栏时,我需要切换 UITableView 以显示搜索到的内容。在背景中,我有一张地图。就像 iPhone 的地图应用程序一样。有什么想法吗?
您可以在地图视图前面添加一个 table视图。显示地图时,table 视图将默认隐藏。当用户搜索时,您可以取消隐藏 table 视图并刷新结果。如果用户取消,那么您将再次隐藏 table 视图。
添加 table 视图作为地图视图的子视图:
- (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];
}
使用以下 UISearchBarDelegate 方法:
- (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];
}
当用户点击取消按钮时:
- (void)cancelButtonTapped:(id)sender {
self.tableView.hidden = YES;
}
对于键盘,Apple 地图会在滚动 table 视图时隐藏键盘。您可以使用 UIScrollViewDelegate(因为 UITableView 是 UIScrollView 的子类)来实现此目的:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[searchBar resignFirstResponder];
}