如何使用 iPhone SDK 3.0 中的 UISearchDisplayController 实现始终位于顶部的搜索栏

发布于 2024-07-30 09:39:17 字数 1610 浏览 9 评论 0原文

iPhone SDK 3.0 有这个方便的新类“UISearchDisplayController”,它可以轻松创建搜索栏、处理所有用户输入并显示搜索结果。

我将它用于我的新应用程序,但我想更改一件事:

默认情况下,搜索栏应放置在显示搜索结果的 UITableView 的顶部。 因此,当向下滚动结果列表时,搜索栏就会消失。

我想要实现的是让搜索栏始终位于顶部,并且在滚动 TableView 时,搜索栏应保持在原来的位置。 (原因:在我的应用程序中有时会有很多搜索结果,所以有时用户必须向下滚动一段时间,当他意识到必须更改搜索字符串时,我不想强​​迫他一直向后滚动)

我已经做的是将搜索栏添加到 UINavigationController 的视图中,这是我的表视图的“父视图”,而不是直接将其添加到表视图中:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;

[searchBar sizeToFit];
CGFloat searchBarHeight = [searchBar frame].size.height;
CGRect mainViewBounds = delegate.navController.view.bounds;
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
                             CGRectGetMinY(mainViewBounds) + 20,
                             CGRectGetWidth(mainViewBounds),
                             searchBarHeight)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[delegate.navController.view addSubview: searchBar];

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

...但这里是我的问题也就是说,表视图位于搜索栏后面,并且搜索结果的第一个 TableViewCell 始终隐藏在我的搜索栏后面。

有没有办法永久调整 UITableViewController 的 UITableView 大小,以便它从搜索栏下方开始?

还有另一个问题:每次我触摸搜索栏时,UISearchDisplayController 都会自动隐藏导航栏并调整 UITableView 的大小(它扩展到顶部),所以我认为此时 UISearchDisplayController 会对我的自定义大小的 TableView 产生大问题...

非常感谢您的帮助!

The iPhone SDK 3.0 has this handy new class "UISearchDisplayController" which makes it easy to create a search bar, handling all the user input and displaying the search results.

I am using it for my new app, but there is one thing i would like to change:

As a default, the search bar should be put at the top of the UITableView that displays the search results. So when scrolling down the result list, the search bar disappears.

What I would like to achieve is having the search bar always on top and when scrolling the TableView, the search bar should stay where it is. (Reason for this: in my app there are sometimes a lot of search results, so sometimes the user has to scroll down a while, and when he realizes that he has to change his search string, i don't want to force him to scroll all the way back)

What i already did is adding the search bar to the view of the UINavigationController which is the "parent view" of my table view, instead of adding it to the table view directly:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;

[searchBar sizeToFit];
CGFloat searchBarHeight = [searchBar frame].size.height;
CGRect mainViewBounds = delegate.navController.view.bounds;
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
                             CGRectGetMinY(mainViewBounds) + 20,
                             CGRectGetWidth(mainViewBounds),
                             searchBarHeight)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[delegate.navController.view addSubview: searchBar];

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

... but here my problem is, that the table view stays behind the search bar, and there is always the first TableViewCell of the search results, that is hidden behind my search bar.

Is there a way to resize the UITableView of my UITableViewController permanently, so that it starts right under the search bar?

And there's another problem: Everytime i touch the search bar, the UISearchDisplayController automatically hides the Navigation Bar and resizes the UITableView (it expands to the top), so i think that at this point the UISearchDisplayController will have a big problem with my custom sized TableView...

Thanks a lot for any help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余罪 2024-08-06 09:39:17

我没有想到解决方案 - 但我会说,您的担忧似乎通过用户只需点击屏幕顶部并将表格缩放到顶部(以更改搜索词(如果有误)。

I don't have a solution off the top of my head - but I will say the concern you have seems to minimized by the ability for the user to simply tap the top of the screen and have the table zoom to the top (to change search terms if it's wrong).

来日方长 2024-08-06 09:39:17

对于页面上不想滚动的部分,我的一般解决方案是将 UITableView 放在另一个视图中。 该外部视图包含两部分——顶部的一个栏和其下方的一个比整页略短的表格。

My general solution for having bits on the page that I don't want to scroll, is to have the UITableView inside another view. That outer view contains two things -- a bar at the top, and a slightly-shorter-than-full-page table below it.

蓝天白云 2024-08-06 09:39:17

@接口:

UISearchDisplayController < UISearchBarDelegate > *searchDisplayController;

@实现:

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

searchDisplayController.delegate = self;
searchBar.delegate = searchDisplayController;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;

@interface:

UISearchDisplayController < UISearchBarDelegate > *searchDisplayController;

@implementation:

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

searchDisplayController.delegate = self;
searchBar.delegate = searchDisplayController;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
百合的盛世恋 2024-08-06 09:39:17

将此代码放入您的 TableViewController 中:

self.tableView.frame = CGRectMake(0,searchDisplayController?.searchBar.bounds.size.height,320,480);

Put this code in your TableViewController:

self.tableView.frame = CGRectMake(0,searchDisplayController?.searchBar.bounds.size.height,320,480);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文