我有一个收集视图,该收集视图基于从firebase中获取的内容填充了视频或图像。
我遇到了一个问题,当我获取数据并将其添加到单元格时,某些单元格会在[图像的顶部]添加视频。
我仔细检查了下面的代码,似乎无法。找到问题的根源。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Item", for: indexPath) as! CollectionViewCell1
if postArray[indexPath.item].media[0].image != nil { //its an image
let date = ConvertDate(mediaTimestamp: postArray[indexPath.row].media[0].postNum!, isItForP3: false).getDate!
do {
cell.imageView.image = postArray[indexPath.item].media[0].image!
if postArray[indexPath.item].user.profileImageUrl != "" {
let url = URL(string: "\(postArray[indexPath.item].user.profileImageUrl)")
let data = try Data(contentsOf: url!)
if let profileImage = UIImage(data: data) {
cell.profImage.image = profileImage
cell.titleLabel.text = postArray[indexPath.item].user.username
cell.subtitleLabel.text = date
}
} else {
//I add a fake image here
cell.titleLabel.text = postArray[indexPath.item].user.username
cell.subtitleLabel.text = date
cell.profImage.image = UIImage(named: "media")
}
} catch {
print("need to add stuff to this catch")
}
} else { //its a video
let videoURL = postArray[indexPath.item].media[0].videoURL
let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.bounds
cell.layer.addSublayer(playerLayer)
let date = ConvertDate(mediaTimestamp: postArray[indexPath.item].media[0].postNum!, isItForP3: false).getDate!
do {
if postArray[indexPath.item].user.profileImageUrl != "" {
let url = URL(string: "\(postArray[indexPath.item].user.profileImageUrl)")
let data = try Data(contentsOf: url!)
if let profileImage = UIImage(data: data) {
cell.profImage.image = profileImage
cell.titleLabel.text = postArray[indexPath.item].user.username
cell.subtitleLabel.text = date
}
} else {
//I add a fake profile image here
cell.titleLabel.text = postArray[indexPath.item].user.username
cell.subtitleLabel.text = date
cell.profImage.image = UIImage(named: "media")
}
} catch {
print("need to add stuff to this catch")
}
}
print("Completed: ", cell.imageView!)
return cell
}
我该如何解决?
答案 0 :(得分:3)
原因在这里
cell.layer.addSublayer(playerLayer)
您应该清除单元格,因为它已出队
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Item", for: indexPath) as! CollectionViewCell1
cell.layer.sublayers.forEach {
if $0 is AVPlayerLayer {
$0.removeFromSuperlayer()
}
}
,也不要使用
let data = try Data(contentsOf: url!)
对于远程数据抓取,您可以使用SDWebImage
答案 1 :(得分:1)
当你说
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Item", for: indexPath) as! CollectionViewCell1
这意味着您正在重用旧单元格,在这种情况下,旧单元格可能具有AVPlayerLayer
解决此问题的一种方法是使用不同的withReuseIdentifier
if postArray[indexPath.item].media[0].image != nil { //its an image
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImgageCell", for: indexPath) as! CollectionViewCell1
return cell
} else { //its a video
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! CollectionViewCell1
return cell
}
}
或者在重用之前重置单元,即在dequeueReusableCell
之后删除图层