我的UITableViewCell在可重用方面遇到了问题。我将不同类型的数据放入不同的单元格。当数据类型相同时(例如:只有图像),我没有问题,因为新的图像会覆盖旧的图像。但是,当数据类型相同时,它们都会留下来。
我正在寻找完全清除单元格的方法或工具。我尝试了很多方法,比如使用字典直接引用数据并将其删除,但都失败了,因为我不能始终如一地分辨重复使用的单元格中的哪些数据(尤其是在大量滚动时)。其他的事情是
cell.contentView.subviews.removeAll()抛出错误,另一个是
cell.contentView.removeFromSuperView()但这只是使单元可用,并且不会加载任何内容。
我的代码如下,感谢advance的帮助(忽略日期内容)。
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))
}
}发布于 2017-06-27 06:24:35
不清楚代码是在视图控制器中还是在UITableViewCell子类中。在任何情况下,您都希望对像元使用UITableViewCell子类,并在prepareForReuse中执行清理任务
override func prepareForReuse() {
super.prepareForReuse()
imagerView.image = nil
}这将涉及到在重用单元格时从单元格中删除任何不希望带入单元格的数据。从图像视图中清除图像,隐藏不会对所有单元格类型显示的视图,等等。
另一种解决方案是拥有多个单元格类,并根据它负责显示的数据类型加载适当的单元格类。这将简化上面的一些代码,并可能有助于防止重用,但它将需要根据事物的外观进行重大重构。
https://stackoverflow.com/questions/44769457
复制相似问题