UITableViewCell删除内容

时间:2017-06-26 22:15:37

标签: ios swift uitableview swift3

我遇到了UITableViewCell的可重用方面的问题。我有不同类型的数据进入不同的单元格。当数据类型相同时(例如:只有图像)我没有问题,因为新图像覆盖了旧图像。但是当数据类型相同时,它们都会坚持下去。

我正在寻找完全清理细胞的方法或工具。我已经尝试了很多东西,比如词典来直接引用数据并擦除它但是失败了因为我无法一致地告诉哪些数据在重用的单元格中(特别是在滚动很多时)。其他的事情是

 cell.contentView.subviews.removeAll()

抛出错误,另一个是

cell.contentView.removeFromSuperView()

但这只是让细胞变得可用而且没有任何东西加载。

我的代码在下面,感谢提前帮助(忽略日期内容)。

 if dictionary?["type"]! != nil {


                cell.imageView?.layer.borderColor = UIColor.black.cgColor
                cell.layer.borderWidth = 1
                cell.imageView?.layer.cornerRadius = 1


                cell.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 25, height: 25)

                self.tableTextOverLays(i: indexPath.row, type: Int((dictionary?["type"])! as! NSNumber), cell: cell)

                if (indexPath.row == tableFeedCount - 1) && lockoutFeed != true && tableFeedCount == self.allDictionary.count && imageFeedCount == imageDictionary.count {
                    let dictionary = self.allDictionary[indexPath.row]
                    let date = dictionary?["date"] as! Double
                    let date2 = date*100000
                    let date3 = pow(10,16) - date2
                    let date4 = String(format: "%.0f", date3)
                    self.setUpFeed(starting: date4, queryLimit: UInt(11))
                }


            }

            else if (dictionary?["type_"] as! String) == "Image" {
                print("image feed", indexPath.row)



                cell.imageView?.layer.borderColor = UIColor.black.cgColor

                cell.layer.borderWidth = 1
                cell.imageView?.layer.cornerRadius = 1
                cell.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 25, height: 25)
                cell.imageView?.image = imageDictionary[indexPath.row]

                if indexPath.row == tableFeedCount - 1 && lockoutFeed != true && tableFeedCount == self.allDictionary.count && imageFeedCount == imageDictionary.count{
                    let dictionary = self.allDictionary[indexPath.row]
                    let date = dictionary?["date"] as! Double
                    let date2 = date*100000
                    let date3 = pow(10,16) - date2
                    let date4 = String(format: "%.0f", date3)

                    setUpFeed(starting: date4, queryLimit: UInt(11))
                }


            }
            else if (dictionary?["type_"] as! String) == "Video" {
                print("video feed", indexPath.row)



                cell.imageView?.layer.borderColor = UIColor.black.cgColor

                cell.layer.borderWidth = 1

                let imagerView = UIImageView()
                imagerView.layer.cornerRadius = 1
                imagerView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
                imagerView.image = videoDictionary[indexPath.row]

//                let vidUIView = UIView()
//                vidUIView.tag = indexPath.row

                let asf = UITapGestureRecognizer(target: self, action: #selector(ProfileController.playVid2(gestureRecognizer:)))
                asf.numberOfTapsRequired = 2
//                imagerView.addGestureRecognizer(asf)


                let adsf = UITapGestureRecognizer(target: self, action: #selector(ProfileController.playVid(gestureRecognizer:)))
                adsf.numberOfTapsRequired = 1
                adsf.require(toFail: asf)                
//              imagerView.addGestureRecognizer(adsf)

                cell.contentView.addGestureRecognizer(asf)
                cell.contentView.addGestureRecognizer(adsf)

                cell.contentView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)

                cell.contentView.addSubview(imagerView)

                //cell.contentView.addSubview(vidUIView)


                if indexPath.row == tableFeedCount - 1 && lockoutFeed != true && tableFeedCount == self.allDictionary.count && videoFeedCount == videoDictionary.count{
                    let dictionary = self.allDictionary[indexPath.row]
                    let date = dictionary?["date"] as! Double
                    let date2 = date*100000
                    let date3 = pow(10,16) - date2
                    let date4 = String(format: "%.0f", date3)

                    setUpFeed(starting: date4, queryLimit: UInt(11))
                }

            }

1 个答案:

答案 0 :(得分:2)

目前还不清楚该代码是在视图控制器中还是在UITableViewCell子类中。在任何情况下,您都希望为您的单元格使用UITableViewCell子类,并在prepareForReuse中执行清理任务:

override func prepareForReuse() {
    super.prepareForReuse()

    imagerView.image = nil
}

这将涉及从细胞中删除您不希望在重复使用时携带到细胞中的任何数据。清除图像视图中的图像,隐藏不会显示所有单元格类型的视图等。

另一种解决方案是拥有多个单元类,并根据负责显示的数据类型加载适当的单元类。这将简化上面的一些代码并可能有助于防止重用,但它需要一个重要的重构事物。