在UITableView中显示和隐藏特定的单元格类型(可能带有动画) [英] showing and hiding a specific cell type in a UITableView (possibly with animation)
问题描述
我有一个UITableView并且已经看到了这个效果,并希望为我们的followind数据实现它:
I have a UITableView and have seen this effect and would like to implement it for our the followind data:
menu_header
menu_subheader
* item
* item
menu_subheader
* item
* item
* item
基本上,我只想显示标题和子标题,然后当用户点击其中一个子标题时,它会显示项目(最好是在动画中)块)并适当地向下或向上调整其他单元格。像这样:
Basically, I would like to show just the header and subeaders and then when the user clicks one of the subheaders, it displays the items (preferably in an animation block) AND adjusts the other cells down or up appropriately. Like this:
是否有预制组件可以执行此操作?考虑到这一点,似乎我想将这些项目单元格设置为隐藏。我已经看过这个 https://github.com/peterpaulis/StaticDataTableViewController ,但看起来它没有使用动态数据。看起来这应该很简单。有关如何完成这项工作的任何想法?理想情况下,我希望它能够在您单击它时插入数据然后如果您单击另一个子标题,关闭另一个子标题并添加到该子标题。
Is there a prebuilt component that does this? Thinking about it, it seems like I would like to set these item cells to be hidden. I have seen this https://github.com/peterpaulis/StaticDataTableViewController but it looks like it doesn't work with dynamic data. It seems like this should be really simple. Any ideas on how to get this done? Ideally, I'd like it to be able to when you click it insert the data and then if you click another sub-header, close the other one and add to that sub-header.
推荐答案
要在表格视图中实现折叠,您有两种选择:
To implement "folding" in a table view you have two options:
- 根据每个部分的折叠/展开属性控制部分中的单元格数。折叠或展开时,使用tableView上的
insert
或deleteRowsAtIndexPaths:withRowAnimation:
方法。 - 使用委托方法控制单元格的高度。根据每个部分的折叠/展开属性,折叠部分返回零。折叠或展开时,调用
beginUpdates
,然后立即使用endUpdates
重新计算高度并为新布局设置动画。
- Control the number of cells in a section based on a folded/unfolded property per section. When folding or unfolding, use the
insert
ordeleteRowsAtIndexPaths:withRowAnimation:
methods on the tableView. - Control the height of the cells using the delegate method. Return zero for folded sections based on a folded/unfolded property per section. When folding or unfolding, call
beginUpdates
followed immediately byendUpdates
to re compute the heights and animate to the new layout.
我在这个GitHub回购。如果您对此有其他疑问,请与我们联系。
I've created a simple implementation of the second option in this GitHub repo. Please let me know if you have other questions about it.
这篇关于在UITableView中显示和隐藏特定的单元格类型(可能带有动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!