Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Can';t在可重用的tableview单元格上从superview中删除视图_Ios_Swift_Uitableview - Fatal编程技术网

Ios Can';t在可重用的tableview单元格上从superview中删除视图

Ios Can';t在可重用的tableview单元格上从superview中删除视图,ios,swift,uitableview,Ios,Swift,Uitableview,我试图删除在可重用tableview单元格中使用标记创建的视图 然而,当我第一次重用UIButton和UIImageview(tableview的5.部分)时,我仍然可以看到,然后它开始正确地删除 为什么不在第一次重用时删除它们?我想在您的情况下重用可能意味着为一个单元格添加两次图像视图和按钮。不过,您只删除其中一个。我认为您应该采取不同的方法(如@vadian所述的不同原型单元),但现在(假设我的假设是正确的),您可以尝试以下方法来解决您的问题: 替换 func tableView(_ tab

我试图删除在可重用tableview单元格中使用标记创建的视图

然而,当我第一次重用UIButton和UIImageview(tableview的5.部分)时,我仍然可以看到,然后它开始正确地删除


为什么不在第一次重用时删除它们?

我想在您的情况下重用可能意味着为一个单元格添加两次图像视图和按钮。不过,您只删除其中一个。我认为您应该采取不同的方法(如
@vadian
所述的不同原型单元),但现在(假设我的假设是正确的),您可以尝试以下方法来解决您的问题:

替换

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: EventCommentsCustom = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EventCommentsCustom
       guard let release = array[exist: indexPath.section] else { return cell }

    if release.user == "condition" {

                let image = UIImage()
                let imageView = UIImageView(image: image)
                imageView.sd_setImage(with: URL(string: "https://example.com/" + TegKeychain.get("profile_pic")!))
                imageView.frame = CGRect(x: 20, y: 10, width: 50, height:50)
                imageView.layer.borderWidth = 0.4
                imageView.layer.masksToBounds = false
                imageView.layer.borderColor = UIColor.gray.cgColor
                imageView.layer.cornerRadius = 25
                imageView.clipsToBounds = true
                imageView.tag = 3
                cell.addSubview(imageView)

                let button = UIButton(frame: CGRect(x: 90, y: 10, width: 200, height: 50))
                button.contentHorizontalAlignment = .left
                button.setTitleColor(UIColor.lightGray, for: .normal)
                button.setTitle(NSLocalizedString("Say something...", comment: ""), for: .normal)
                button.addTarget(self, action: #selector(EventComments.openInput), for: .touchUpInside)
                button.tag = 3
                cell.addSubview(button)

    } else {

    if let viewWithTag = cell.viewWithTag(3) {
                    if viewWithTag is UIImageView {
                        print("DONE")
                        viewWithTag.removeFromSuperview()
                    }
                }
                if let viewWithTag = cell.viewWithTag(3) {
                    if viewWithTag is UIButton {
                        print("DONE")
                        viewWithTag.removeFromSuperview()
                    }
                }
    }

 return cell
}

if let viewWithTag = cell.viewWithTag(3) {
    if viewWithTag is UIImageView {
        print("DONE")
        viewWithTag.removeFromSuperview()
    }
}
if let viewWithTag = cell.viewWithTag(3) {
    if viewWithTag is UIButton {
        print("DONE")
        viewWithTag.removeFromSuperview()
    }
}
更新- 不同单元类型的方法如下所示:

while let viewToRemove = cell.viewWithTag(3) {
    if viewToRemove is UIImageView || viewToRemove is UIButton {
        viewToRemove.removeFromSuperview()
    }
}

为什么不使用不同的单元呢?您是否知道您可以在表视图中设计多个自定义原型单元(从Xcode 6开始)?
viewWithTag
舞蹈已经过时了。@vadian我也在想同样的事情。我是否应该将多个原型单元添加到DynamicTableView并设计它们,但我如何处理cellforrowat?是的,您应该这样做。您可以通过标识符和索引路径来区分单元格。@瓦迪安有时间讲一个简短的例子吗?不客气。尽管如此,还是要考虑对不同的细胞类型使用不同的原型。让你的生活更轻松@UtkuDalmaz,如果你能将答案标记为正确,那就太好了,如果它能帮助你……;)我“试图为同一索引路径将多个单元格出列,这是不允许的。”?这正是你告诉我们的。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let release = array[exist: indexPath.section] else { return cell }

    if release.user == "condition" {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OneIdentifier", for: indexPath) as! OneCustomCellType
        // configure your cell
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AnotherIdentifier", for: indexPath) as! AnotherCustomCellType
        // configure your cell
        return cell
    }
}