简体   繁体   English

如何在UITableViewCell上保留子视图

[英]How to keep subview on UITableViewCell

When the user taps the accessory view, the profile image in the UITableViewCell gets darkened by a black view that gets added on as a subview. 当用户点击附件视图时,UITableViewCell中的配置文件图像会被添加为子视图的黑色视图变暗。

Here is what the cell looks like with the black subview: 这是带有黑色子视图的单元格外观:

在此处输入图片说明

Here's the issue: When I tap on another cell, the subview gets removed from the first cell and added to the second: 问题是:当我点击另一个单元格时,子视图将从第一个单元格中删除,并添加到第二个单元格中:

在此处输入图片说明

I would like to keep the subview for all cells that have been tapped. 我想保留所有已被点击的单元格的子视图。

Here is the code in which I handle that functionality: 这是我处理该功能的代码:

 self!.profileImageBlackView.cornerRadius = cell.followUserImage.frame.height/2
 self!.profileImageBlackView.frame = cell.followUserImage.frame
 cell.followUserImage.addSubview(self!.profileImageBlackView)
 cell.followButton.hidden = false

For some reason, the follow button gets added to both cells, but the "profileImageBlackView" gets moved from cell to cell depending upon which one was activated. 由于某些原因,在两个单元格中都添加了“跟随”按钮,但是“ profileImageBlackView”在一个单元格之间移动,具体取决于激活了哪个单元格。

You cannot display the same view in different cells. 您不能在不同的单元格中显示相同的视图。 If you add the view to another cell it's removed from the first one. 如果将视图添加到另一个单元格,则将其从第一个单元格中删除。 You must create a separate view for each cell. 您必须为每个单元格创建一个单独的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM