点击UITableView Cell时隐藏标签

时间:2021-08-18 20:29:29

I have a label within my cells that I would like to show/hide when tapped. I know that I have to get the index path of the cell that was tapped from within didSelectRowAtIndexPath. But I am unsure of how I then show/hide the label in that particular cell.

我的单元格中有一个标签,我想在点击时显示/隐藏。我知道我必须从didSelectRowAtIndexPath中获取单元格的索引路径。但我不确定如何在特定单元格中显示/隐藏标签。

Am I able to show/hide it from within didSelectRowAtIndexPath, or is there a way to deal with it in cellForRowAtIndexPath and then update it?

我可以在didSelectRowAtIndexPath中显示/隐藏它,还是有办法在cellForRowAtIndexPath中处理它然后更新它?

I did some research into this but I really couldn't find a whole lot.

我做了一些研究,但我真的找不到很多东西。

Here is all I have so far:

这是我到目前为止所有的一切:

var selectedRowIndex: NSIndexPath = NSIndexPath(forRow: -1, inSection: 0)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedRowIndex = indexPath
}

1 个解决方案

#1


10  

Here is how you get to the cell from the index path:

以下是从索引路径到达单元格的方法:

let cell = tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
cell.myTextLabel.hidden = true

Also, depending on your needs, you might want to deselect the cell as well.

此外,根据您的需要,您可能还想取消选择单元格。

tableView.deselectRowAtIndexPath(indexPath)

#1


10  

Here is how you get to the cell from the index path:

以下是从索引路径到达单元格的方法:

let cell = tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
cell.myTextLabel.hidden = true

Also, depending on your needs, you might want to deselect the cell as well.

此外,根据您的需要,您可能还想取消选择单元格。

tableView.deselectRowAtIndexPath(indexPath)