简体   繁体   English

如何实现高级表视图头

[英]How to achieve an advanced table view header

I have a grouped UITableView . 我有一个分组的UITableView Now unlike the default table view header my header should be next to the individual cells: 现在,与默认的表格视图标题不同,我的标题应该在各个单元格旁边

Standard: 标准:

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

What I need 我需要的

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

Some remarks: 一些说明:

  • Group H1 has three items H1组有三个项目
  • Group H2 has two items H2组有两个项目
  • Group H3 has just one item (I have to insert a placeholder cell so that the header cell can have the full height of two item-cells H3组只有一个项目(我必须插入一个占位符单元格,以便标题单元格可以具有两个项目单元格的完整高度
  • When the user scrolls the list then the header should be pushed upwards that the two header-cells don't overlap. 当用户滚动列表时,应将标题向上推,以使两个标题单元格不重叠。

The challenge here is the height of the header cell: 这里的挑战是标题单元格的高度:

  • If I set the height to 0 and uncheck clip subviews then that header cell is shown but does not get pushed away at the correct position. 如果将高度设置为0并取消选中clip subviews则将显示该标题单元格,但不会在正确的位置被推开。
  • If I set the height of the header cell to the visual height then there is an empty space of that height across the whole width of the table which I don't want... 如果我设置标题单元格的高度视觉高度,然后对面有,我不希望表的整个宽度是高度的一个空的空间...

Update 1: I just realized that apple uses such a list for the search (grouped by messages, mails, contacts, calenders etc.). 更新1:我刚刚意识到苹果使用这样的列表进行搜索(按消息,邮件,联系人,日历等分组)。 So my question basically is how can I tweak UITableView in order to behave like the grouped search results... :-) 所以我的问题基本上是如何调整UITableView以便表现得像分组的搜索结果... :-)


(source: mshcdn.com ) (来源: mshcdn.com

Update: I created a project on github, which does exactly this. 更新:我在github上创建了一个项目,它就是这样做的。 I have extracted all the relevant code for the behaviour into two classes ( BBFloatingHeaderViewController & BBFloatingHeaderCell ). 我已经将行为的所有相关代码提取到两个类中( BBFloatingHeaderViewControllerBBFloatingHeaderCell )。 There is also an example project. 还有一个示例项目。 I hope this is useful for others :-) 我希望这对其他人有用:-)

Here's the project: besi/FloatingTableViewHeader 这是项目: besi / FloatingTableViewHeader

This is an excerpt from the readme: 这是自述文件的摘录:

Floating UITableView headers 浮动UITableView标头

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. 这些类旨在复制在iOS内置Spotlight搜索中发现的行为,该行为将搜索结果按类别分组,并且相应类别的图标浮在搜索结果的左侧。

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. BBFloatingHeaderViewControllerBBFloatingHeaderCell.m + .h文件添加到您的项目中。
  2. Create a TableView and set the Class of the ViewController to your subclass of BBFloatingHeaderViewController 创建一个TableView并将ViewController的类设置为BBFloatingHeaderViewController的子类
  3. Create the header cell in IB and set its class to your subclass of BBFloatingHeaderCell 在IB中创建标头单元,并将其类设置为BBFloatingHeaderCell的子类
  4. Make sure that your floating header view is the topmost view in the BBFloatingHeaderCell 's view hierarchy. 确保浮动标题视图是BBFloatingHeaderCell的视图层次结构中的最顶层视图。

The result 结果

Floating Headers http://i.minus.com/jyea3I5qbUdoQ.png 浮动标头http://i.minus.com/jyea3I5qbUdoQ.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM