gpt4 book ai didi

swift - 我正在 Collection View 中的 AVPlayer 中加载视频,但它会重复一些单元格数据

转载 作者:行者123 更新时间:2023-11-28 11:32:52 26 4
gpt4 key购买 nike

我正在 AVPlayer 的 Collection View 中加载视频。一些单元格重复数据。如何解决这个问题?

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell:homecollCell = self.collectionview.dequeueReusableCell(withReuseIdentifier: "homecollCell", for: indexPath) as! homecollCell
//cell.player?.pause()

let obj = self.friends_array[indexPath.row] as! Home

cell.img.sd_setImage(with: URL(string:self.appDelegate.imgbaseUrl!+obj.thum), placeholderImage: UIImage(named: ""))

let url = URL.init(string: self.appDelegate.imgbaseUrl!+obj.video_url)

let screenSize: CGRect = UIScreen.main.bounds
cell.playerItem = AVPlayerItem(url: url!)
//cell.player!.replaceCurrentItem(with: cell.playerItem)
cell.player = AVPlayer(playerItem: cell.playerItem!)
cell.playerLayer = AVPlayerLayer(player: cell.player!)
cell.playerLayer!.frame = CGRect(x:0,y:0,width:screenSize.width,height: screenSize.height)
cell.playerLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
cell.playerView.layer.addSublayer(cell.playerLayer!)

cell.player?.play()

最佳答案

单元格是可重用的,因此您必须使用 prepareForReuse() 删除或放弃您对其他单元格所做的更改。就是这样。在 cell 中调用它。

override func prepareForReuse() {
super.prepareForReuse()
self.label.text = nil
}

关于swift - 我正在 Collection View 中的 AVPlayer 中加载视频,但它会重复一些单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com