iphone - 如何实现高级 TableView 标题

标签 iphone objective-c ios cocoa-touch uitableview

我有一个分组的UITableView。现在,与默认的表格 View 标题不同,我的标题应该位于各个单元格的旁边:

标准:

[Header Cell]
[Item 1]
[Item 2]

我需要什么

+-------+----------------+
|       |  Item 1a       |
|  h1   +----------------+
|       |  Item 1b       |
+ - - - +----------------+
|          Item 1c       |
+------------------------+
|       |  Item 2a       |
|  h2   +----------------+
|       |  Item 2b       |
+------------------------+
|       |  Item 3a       |
|  h3   +----------------+
|       |  -placeholder- |
+-------+----------------+

一些评论:

  • H1 组包含三项
  • H2 组有两项
  • H3 组只有一个项目(我必须插入一个占位符单元格,以便标题单元格可以具有两个项目单元格的完整高度
  • 当用户滚动列表时,标题应向上推,两个标题单元格不会重叠。

这里的挑战是标题单元格的高度:

  • 如果我将高度设置为 0 并取消选中 clip subviews,则该标题单元格会显示,但不会被推到正确的位置。
  • 如果我将标题单元格的高度设置为视觉高度,那么在表格的整个宽度上会出现一个该高度的空白空间,这是我不想要的...<

更新1:我刚刚意识到苹果使用这样的列表进行搜索(按消息、邮件、联系人、日历等分组)。所以我的问题基本上是如何调整 UITableView 以便表现得像分组搜索结果......:-)


(来源:mshcdn.com)

最佳答案

更新:我在 github 上创建了一个项目,它正是这样做的。我已将该行为的所有相关代码提取到两个类中( BBFloatingHeaderViewControllerBBFloatingHeaderCell )。还有一个示例项目。我希望这对其他人有用:-)

这是项目:besi/FloatingTableViewHeader

这是自述文件的摘录:

Floating UITableView headers

These classes aim to copy the behaviour found in iOS built-in Spotlight search, where the search results are grouped by category and the icon of the respective category floats on the left side of the search results.

Setup

Check out the example project so see how to setup the classes.

  1. Add the .m + .h files of BBFloatingHeaderViewController and BBFloatingHeaderCell to your project.
  2. Create a TableView and set the Class of the ViewController to your subclass of BBFloatingHeaderViewController
  3. Create the header cell in IB and set its class to your subclass of BBFloatingHeaderCell
  4. Make sure that your floating header view is the topmost view in the BBFloatingHeaderCell's view hierarchy.

The result

Floating Headers http://i.minus.com/jyea3I5qbUdoQ.png

关于iphone - 如何实现高级 TableView 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475897/

相关文章:

iphone - Titanium 的 HTML5(和 JS)真的是原生的吗?

ios - 如何将出生日期 UIDatePicker 添加到 UITextField?

objective-c - setBackgroundImage forBarMetrics图像大小?

ios - 如何使用 Instruments 和 UI Automation 测试重新排序表格?

ios - 更改 MFMailComposeViewControllers 状态栏样式

iphone - SOAP 响应不完整

iphone - iPhone上的Shark工具崩溃

iphone - iOS 使用 MPMoviePlayerController 播放视频

objective-c - 如何使 NSMutableString 为空

ios - 选项卡索引在 App Delegate 类中正确,但在 App Delegate 对象中不正确