UISearchDisplayController - 如何预加载searchResultTableView [英] UISearchDisplayController - how to preload searchResultTableView

查看:129
本文介绍了UISearchDisplayController - 如何预加载searchResultTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一些默认内容,当用户点击Searchbar,但在输入任何文本之前。

I want to show some default content when the user taps the Searchbar, but before any text is entered.

我有一个解决方案使用settext:

I have a solution working using settext:

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {

   [searchDisplayController.searchBar setText:@" "];

}

这可行,但它不优雅, Searchbar消失。

This works but it's not elegant and the hint text in the Searchbar disappears.

有没有更好的方法来预加载数据到UISearchDisplayController中的SearchResultTableView,而不必在自定义控制器中实现整个UISearch功能?

Is there a better way to preload data to the SearchResultTableView in a UISearchDisplayController, without having to implement the whole UISearch functionality yourself in a custom controller?

对于想要的效果的演示,看看Safari的搜索框,如果你点击它,搜索界面打开,显示以前的搜索。

For a demo of the desired effect, look at Safari's search box, if you tap it, the search interface opens with previous searches showing.

推荐答案

好的,我有它。预加载您的dataSource与一些数据,并做以下黑客(没有违法),你会得到tableView显示。

OK, I have it. Preload your dataSource with some data and do the following hack (nothing illegal) and you'll get the tableView to show up. Note that there may be some clean-up to do, but this will get your default data to display.

在视图控制器的viewDidLoad中,比拥有UISearchBar更好:

In viewDidLoad of the view controller than owns the UISearchBar:

[super viewDidLoad];
[self.searchDisplayController.searchResultsTableView reloadData];
// whatever you named your search bar - mine is property named searchBar
CGRect testFrame = CGRectMake(0, 20, self.searchBar.frame.size.width, 100);
self.searchDisplayController.searchResultsTableView.frame = testFrame;
[self.searchBar.superview addSubview:self.searchDisplayController.searchResultsTableView];

这是发生了什么:

UISearchBar不想显示searchResultsTableView直到您开始编辑。如果你触摸tableView(例如reloadData),它将加载并在那里,坐在内存与frame = CGRectZero。

Here's what's happening:
The UISearchBar doesn't want to show the searchResultsTableView until you start editing. If you touch the tableView (e.g. reloadData) it will load and be there, sitting in memory with frame = CGRectZero. You give it a proper frame and then add it to the superview of the searchBar, et voila.

我通过显示searchBar和superview的superview来验证这是正确的的tableView 之后适当加载tableView - 它们具有相同的superview。所以,我回去,并将tableView早期添加到superview,并确定足够显示。对我来说,它显示为暗淡(也许另一个视图上面它?alpha设置更低?),但仍然可点击。

I verified this is correct by displaying the superview of the searchBar and the superview of the tableView after a proper load of the tableView - they have the same superview. So, I go back and add the tableView to the superview early, and sure enough it shows up. For me, it showed up dimmed (maybe another view above it? alpha set lower?), but was still clickable. I didn't go any further, but that definitely gets you your tableView displaying without any user interaction.

享受,

Damien

这篇关于UISearchDisplayController - 如何预加载searchResultTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆