iPhone:默认隐藏 UITableView 搜索栏 [英] iPhone: Hide UITableView search bar by default

查看:28
本文介绍了iPhone:默认隐藏 UITableView 搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Interface Builder 创建了一个表视图,我向其中添加了库的搜索栏和搜索显示控制器以添加搜索功能.但是,IB 将其设置为在第一次显示视图时在屏幕顶部可见栏.

我想知道如何在默认情况下隐藏搜索栏,但仍然可以使用表格视图滚动(例如,请参阅 Apple 的邮件应用程序).我试过在 viewDidLoad 中调用 scrollRectToVisible:animated: 来向下滚动表格视图,但无济于事.默认情况下隐藏搜索栏的首选方式是什么?

解决方案

首先确保将 UISearchBar 添加到 UITableView 的 tableHeaderView 中,以便它随着表格的内容滚动,而不是固定在表格的顶部查看.

搜索栏不计为 tableview 中的一行,因此如果您将 tableview 的顶部滚动到第一行,它会隐藏"搜索栏:

[yourTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop 动画:NO];

或在 Swift 中:

yourTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, 动画: false)

确保在包含数据之前不要滚动 tableview(如果给定的 indexPath 未指向有效行(即如果 tableview 为空),scrollToRowAtIndexPath 将引发异常).

I used the Interface Builder to create a table view, to which I added the library's Search Bar and Search Display Controller to add search functionality. However, IB set it up so that the bar is visible at the top of the screen when the view is first displayed.

I'd like to know how to have the search bar be hidden by default but still scrollable with the table view (see Apple's Mail application for an example). I've tried calling scrollRectToVisible:animated: in viewDidLoad to scroll the table view down, but to no avail. What's the preferred way of hiding the search bar by default?

解决方案

First make sure, to add the UISearchBar to the tableHeaderView of the UITableView so that it gets scrolled with the table's content and isn't fixed to the top of the view.

The searchbar isn't counted as a row in the tableview, so if you scroll the top of the tableview to the first row, it 'hides' the searchbar:

[yourTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

or in Swift:

yourTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Make sure to not scroll the tableview before it contains data (scrollToRowAtIndexPath will raise an exception if the given indexPath does not point to a valid row (i.e. if the tableview is empty)).

这篇关于iPhone:默认隐藏 UITableView 搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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