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

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

问题描述

我使用Interface Builder创建了一个表视图,我在其中添加了库的搜索栏和搜索显示控制器以添加搜索功能。但是,IB会对其进行设置,以便在首次显示视图时在屏幕顶部显示条形。

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.

我想知道如何使用搜索栏默认隐藏但仍可使用表格视图滚动(有关示例,请参阅Apple的Mail应用程序)。我已经尝试在 viewDidLoad 中调用 scrollRectToVisible:animated:来向下滚动表视图,但无济于事。隐藏搜索栏的首选方法是什么?

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?

推荐答案

首先确保将UISearchBar添加到tableHeaderView中UITableView使其与表的内容一起滚动,并且不固定在视图的顶部。

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.

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

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];

或在Swift中:

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

确保在包含数据之前不滚动tableview( scrollToRowAtIndexPath 如果给定的indexPath没有指向a,则会引发异常有效行(即如果tableview为空))。

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天全站免登陆