代码之家  ›  专栏  ›  技术社区  ›  Utku Dalmaz

无法从可重用TableView单元格的SuperView中删除视图

  •  -2
  • Utku Dalmaz  · 技术社区  · 7 年前
    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
    }
    

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

    但是,当第一次重复使用时,我仍然可以看到uibutton和uiimageview(5)。然后它开始正确地删除

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   André Slotta    7 年前

    我想,在您的情况下重用可能意味着图像视图和按钮会为一个单元添加两次。不过,您只能删除其中一个。我认为你应该采取不同的方法(像不同的原型细胞 @vadian 声明)考虑到,但目前(假设我的假设是正确的),你可以尝试这样来解决你的问题:

    替换…

    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()
        }
    }
    

    更新 - 不同单元格类型的方法如下所示:

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