【发布时间】:2016-12-06 03:08:57
【问题描述】:
我是 swift 新手,使用 swift 3,我构建了读取一组图像的 tableview,我希望它如下所示: 当用户在表格视图中选择图像时,我将其显示在 UIImageView 中,当取消选择他已经在表格视图中选择的图像时,将其从 UIImageView 中删除(启用了多选)。我下面的代码仅适用于一个图像选择,但是如果选择了多个图像然后取消选择最后一个选择的图像,则只会从 UIImageview 中删除。所以我的问题是如何删除属于 UItableview 中取消选择记录的 UIImageview
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
image_view.image = UIImage(data: Saveddata[indexPath.row].photo as! Data)
//create image view programmatically
currentImageView = UIImageView(frame: CGRect(x: 130 + (i * 10), y: 50 + (i * 5), width: 50, height: 100))
currentImageView.contentMode = .scaleAspectFit
currentImageView.image = image_view.image
view.addSubview(currentImageView as UIView)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
currentImageView.removeFromSuperview()
}
【问题讨论】:
-
您不能简单地删除
UITableViewCell中的视图或 UI 组件。它可重复用于其他行。您可以做的是使用全局数组设置一个标志,并保留indexPath.row作为您已删除的那些的索引。然后在您的cellForRow中,检查该索引是否出现,隐藏图像或将width约束更改为 0 -
避免使用
addSubview,而是使用 Storyboard 或带有 IBOutlet 的 xib。这可以防止在单元格内创建重复视图 -
@h44f33z,谢谢,但是我并没有尝试将其从 tableview 中删除,而是尝试从我创建的 UIview 中删除它,并在 tableview 中选择此图像时将图像添加到其中
-
尝试使用
tag。在下面添加了我的答案
标签: swift