xcode 点击 UITableView Cell 时隐藏标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26425302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:53:53  来源:igfitidea点击:

Hide label when UITableView Cell is tapped

iosxcodeuitableviewswift

提问by user3746428

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 cellForRowAtIndexPathand 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
}

回答by Mundi

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)