如何在swift中显示和隐藏表格视图单元格

我有一个tableView与两个不同的自定义单元格。 一个单元格的文本字段数据很less,另一个单元格也有不同的文本字段数据,这里我需要折叠并隐藏每个单元格。 我不知道该怎么做。请让我知道。

An Open Source iOS Control Allowing You To Create Great Looking Collapsible Lists Without UITableView

请参考上面的链接,我想同样在迅速。

提前致谢

所以你想隐藏你的一个UITableViewCells?

我认为最简单的方法是添加更多的信息到你的“行数据数组”。

例如:

你有10个列,你想隐藏4个特定的列。 你有一个叫做“getRowData”的函数 – 这个返回(当没有filter被选中时)所有10行。 当你点击一个button,你可以改变你的“getRowData”函数只返回你想要的4行。

用上面展示的例子 – 你需要一些基于UITableViewController的可折叠列表。

所以你有(在这个例子中)3主要类别 – 如果用户点击一个“标题”,你想展开这个类别的列表。

所以你可以使用部分 – 为每个类别创build一个部分。 然后执行这样的事情:

//为你的“激活类别”创build一个主variables

var enabledCategory:Int = 0 // you could add i base value override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let indexPath = tableView.indexPathForSelectedRow(); enabledCategory = indexPath.row; .... 

在这里检查哪个部分被用户点击。 如果(例如“0” – 所以第一个,扩大这个部分。所以做这样的事情:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if(indexPath.section == enabledCategory) { return rows // return rows you want to show } 

您还需要更改此处的行数:

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } 

这应该是(在你的例子)总是3。

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

这取决于你的部分//类别