Swift3:UITableViewCell和UIButton – 单击UIButton显示和隐藏UITableViewCell

我是Swift 3.0新手,目前正在开发一个关于IOS app的学校项目。

我在UITableViewCellUIButton的链接方面遇到了困难。

这就是我的应用程序的样子

基本上,我要做的是当我点击UIButton时,它会将单元格的高度(日记和趋势)降低到0(隐藏单元格)并增加单元格的高度(分享和如何使用BP监控器)(显示单元格) – 假设现在“隐藏”单元格(共享和如何使用BP监视器)。

欣赏是否有一个例子可以遵循。

非常感谢你 :)

*在尝试了@Sandeep Bhandari给出的例子之后

这是我的ViewController中的代码:

class TableViewController:UITableViewController {var expandedCellIndex:IndexPath? =没有

 override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell : UITableViewCell! = nil if indexPath == expandedCellIndex { cell = tableView.dequeueReusableCell(withIdentifier: "expandedCell") as! ExpandedTableViewCell (cell as! ExpandedTableViewCell).label1.text = "shown" (cell as! ExpandedTableViewCell).label2.text = "Efgh" (cell as! ExpandedTableViewCell).label3.text = "xyz" } else { cell = tableView.dequeueReusableCell(withIdentifier: "simpleCell") as! SimpleTableViewCell (cell as! SimpleTableViewCell).label.text = "abcd" } return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if self.expandedCellIndex == indexPath { self.expandedCellIndex = nil } else { self.expandedCellIndex = indexPath } self.tableView.reloadData() } override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 140 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

我无法得到与建议的答案相同的输出。

我的输出

非常感谢!

这是一个简单的代码,它不仅可以扩展特定的单元格,还可以扩展所有单元格。 如果您只想扩展一个单元格,也可以这样做。

步骤1:

在故事板中声明两个动态原型单元。 在此处输入图像描述

让我们将红色调用为SimpleCell,将绿色调用为ExpandedCell。

第2步:

为每个单元格添加可重用的标识符。

在此处输入图像描述

在此处输入图像描述

第3步

为这两个单元格创建自定义类,并为标签创建IBOutlets。

步骤4:

现在在你的VC的ViewDidLoad中写这个。

 override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 140 } 

第5步:

假设一次只能扩展一个单元格我声明单个IndexPathVariable,如果要扩展多个单元格,则可以始终声明数组。

 var expandedCellIndex : IndexPath? = nil 

第6步:

编写tableView数据源代码。

override func numberOfSections(在tableView:UITableView中) – > Int {return 1}

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell : UITableViewCell! = nil if indexPath == expandedCellIndex { cell = tableView.dequeueReusableCell(withIdentifier: "expandedCell") as! ExpandedTableViewCell (cell as! ExpandedTableViewCell).label1.text = "abcd" (cell as! ExpandedTableViewCell).label2.text = "Efgh" (cell as! ExpandedTableViewCell).label3.text = "xyz" } else { cell = tableView.dequeueReusableCell(withIdentifier: "simpleCell") as! SimpleTableViewCell (cell as! SimpleTableViewCell).label.text = "abcd" } return cell } 

第7步:

我正在细胞水龙头上扩展细胞你也可以在按钮水龙头上做:)在IBAction中按钮lemme写这个逻辑把它写在didSelectRow中。

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if self.expandedCellIndex == indexPath { self.expandedCellIndex = nil } else { self.expandedCellIndex = indexPath } self.tableView.reloadData() } 

结论:

在此处输入图像描述

在此处输入图像描述

放弃

我正在使用动态单元格高度,因此没有写入heightForRowAtIndexPath,因为将自动计算单元格的高度。

如果在单元格中使用没有隐式大小的任何UIComponents,则可能必须实现heightForRowAtIndexPath,如下所示。

 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if self.expandedCellIndex == indexPath { return expnadedCellHeight } else { return simpleCellHeight } }