如何在 swift 2 中的单元格内增加按钮点击操作的行高

How to increase row Height on button tapp action inside the cell in swift 2

我是 swift 的新手,我有一个 table 行,其中包含 2 个标签(名称和说明)和右侧的一个按钮。单击按钮时,我想在描述的底部显示一些控件。

我正在使用自动调整大小技术来动态调整行中的内容,方法是使用以下代码。

tableView.rowHeight = IUTableViewAutomaticDimension

我的描述文本是多行的(取决于文本 1 到 5)数据在我的 table 视图中显示得很好。但是在按钮上点击行高不会自动增加。以下是点击按钮中的代码。

lblmoreDescrption.hidden = false

我正在使用 Xcode 7 和 swift 2。

谢谢

在进行更改后点击按钮试试这个。您可以将此 objective c 代码转换为 swift.

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

在Swift中:

    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
    tableView.endUpdates()

你需要这个:

self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()

其中 indexPath 是您要重新加载的单元格的索引路径。

首先,请为您在tableView: cellForRowAtIndexpath indexPath:

中添加的按钮分配标签

在按钮点击事件中,喜欢:

@IBAction func buttonClick(sender:UIButton!) {

    self.tblName.beginUpdates()
    self.tblName.reloadRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection: 0)]) , withRowAnimation: UITableViewRowAnimation.Fade)
    self.tblName.endUpdates()
}

您可以在一个 UIView 中定义您的控件,您可以为该 uiview 的高度限制获取 iboutlet。并且您需要将其设置为 0,当单击单元格内的按钮时,您需要将其设置为适当的高度,然后在索引路径重新加载该行。

例如。

    Iboutlet UIVIEW *viewCustomControls; // This contains your extra controls which you want to show on the button clicked.
   Iboutlet NSLayoutConstraints *cons_height_viewControl; // This is the constraint outlet for the viewCustomControls Height.

当您在点击事件中点击按钮时...

{
if (need to expands)
{
      cons_height_viewControl.constants = 100 // To expand, Here is approximate value you can make as you want.
}else{
      cons_height_viewControl.constants = 0 // To collapse
    }
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths:(arrIndexPaths withRowAnimation:UITableViewRowAnimation.Fade)
self.tableView.endUpdates()
}