UITableView中的UISearchBar [英] UISearchBar in a UITableView

查看:100
本文介绍了UITableView中的UISearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模仿类似于iPod应用程序(适用于Artists)的表格视图的行为-这是一个分区的表格视图,其右侧具有分区索引,顶部带有搜索栏,但最初在显示视图时隐藏

I'm trying to mimic the behaviour of a table view similar to the iPod app for Artists - it's a sectioned table view with a section index on the right, with a search bar at the top, but initially hidden when view shown.

我正在使用sdk 3.1.2和IB,因此只需将UISearchDisplayController拖到我的NIB中-它会连接所有内容以进行搜索.问题开始是因为我将UISearchBar添加到UITableView的第一部分,因为如果我正确理解我必须这样做,那么我可以通过直接触摸部分索引中的搜索图标来跳到搜索栏吗?出现表格视图时,我看到了搜索栏,但它的大小已经调整,现在顶部的节索引后面有一个白色的块.它没有采用UISearchBar周围的颜色,有趣的是与Interface Builder中显示的颜色不同.

I am using sdk 3.1.2 and IB, so simply dragged a UISearchDisplayController into my NIB - it wires everything up for searching. The problem starts because I'm adding the UISearchBar to the first section of the UITableView, because if I understand correctly I must do this so I can jump to the search bar by touching the search icon in the section index directly? When the table view appears I see the search bar but it has resized and I now have a white block behind the section index at the top. It doesn't take the color of the UISearchBar's surround which interestingly is different to that shown in Interface Builder.

我发现了一个技巧,可以在UIView中添加一个小的导航栏和一个UISearchBar,然后将其添加到表格视图单元格中.这可以工作,但是导航栏背景的颜色是您通常期望的颜色(灰色),而不是如上所述的其他颜色.更有意思的是,如果我点击搜索栏开始搜索,然后点击取消",则所有操作都是固定的.当搜索栏位于整个tableview单元格的背景相同吗?

I found a tip to add a small navigation bar and a UISearchBar in a UIView, then add this to the table view cell. This works, but the color of the navigation bar's background is what you'd expect normally (gray), not the different color as noted above. More interesting, if I tap the search bar to start a search, then tap Cancel, all is fixed. The background along the whole tableview cell when the search bar is, is the same?

推荐答案

回答了我自己的问题,但可能对其他初学者有所帮助:

Answered my own question, but might be helpful to other beginners:

  • 加载视图时,将搜索栏放在表格视图标题中,滚动contentOffset降低此搜索栏的高度(通常为44像素),但易于动态检查
  • 在带有"{search}"的部分索引中添加搜索图标
  • 将sectionIndexSection设为搜索-1
  • 处理部分索引时,请使用特殊"索引值进行搜索"-1"(将contentOffset滚动回(0,0)

更新:使用字符串常量

Update: use the string constant UITableViewIndexSearch string in place of the undocumented string "{search}" - i.e. return it as one of the array items when implementing:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

查看全文

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