gpt4 book ai didi

ios - 表格 View 单元格中的播放按钮显示错误的视频,因为它没有获得正确的单元格

转载 作者:行者123 更新时间:2023-11-28 08:13:14 24 4
gpt4 key购买 nike

我有一个表格 View Controller ,我在其中通过单击单元格中的按钮播放视频。

当我尝试在第一个单元格中播放视频时,它会在第二个单元格中播放视频。

我假设问题是因为它无法识别播放视频的正确单元格。

以下是我的代码

var cell : ViewTableCell?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{


let resusableIdentifier: String = "ViewControllerCell"
cell = (tableView.dequeueReusableCell(withIdentifier: resusableIdentifier) as? ViewTableCell)

if cell == nil {
cell = ViewTableCell(style:UITableViewCellStyle.default, reuseIdentifier: resusableIdentifier)
}

cell?.videoName.text = ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoTitle


cell?.playButton.addTarget(self, action: #selector(TrendViewController.onTapPlayButton), for: .touchUpInside)

cell?.playButton.tag = indexPath.row

cell?.videoView.bringSubview(toFront: (cell?.videoName)!)
return cell!
}
func onTapPlayButton(sender: subclassedUIButton){

let indexOfButton = sender.indexPath
let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[indexOfButton!].videoUrl, ofType:"mp4")

let path = NSURL.fileURL(withPath: url!)
let currentCell = trendTableView.cellForRow(at: indexOfButton) as! ViewTableCell
currentCell.videoPlayerItem = AVPlayerItem.init(url: path)
currentCell.startPlayback()

}

当我在 cellForRowAt indexPath 函数中播放时,上面的代码可以正确播放视频

当我尝试通过点击按钮播放时,它会在不同的单元格中播放视频。

同样在 onTapPlayButton() 中我得到以下错误

无法转换“Int”类型的值?到预期的参数类型 'IndexPath'

任何帮助将不胜感激。谢谢。

下面是更新后的代码

class ViewTableCell: UITableViewCell {
var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?
var paused: Bool = false
var videoPlayerItem: AVPlayerItem? = nil {
didSet {

avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
}
}

var tapAction: ((ViewTableCell) -> Void)?

@IBAction func playButtonAction(_ sender: UIButton) {
tapAction?(self)
}
func setupMoviePlayer(view:UIView){
self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
avPlayer?.volume = 3
avPlayer?.actionAtItemEnd = .none
avPlayerLayer?.frame = view.bounds
self.backgroundColor = .clear
view.layer.insertSublayer(avPlayerLayer!, at: 0)

let interval = CMTime(value: 1, timescale: 2)
avPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using :{ (progressTime) in
let seconds = CMTimeGetSeconds(progressTime)

if let duration = self.avPlayer?.currentItem?.duration{
let durationSeconds = CMTimeGetSeconds(duration)
self.videoSlider.value = Float(seconds/durationSeconds)
}
})

}

func stopPlayback(){
self.avPlayer?.pause()
}

func startPlayback(){
self.avPlayer?.play()
}

override func awakeFromNib() {
super.awakeFromNib()
self.setupMoviePlayer(view: videoView)
// Initialization code
}
}

广告 View Controller 类

var cell : TrendViewTableCell?
var currentCell = TrendViewTableCell()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//MARK CellForRowAt


let resusableIdentifier: String = "ViewControllerCell"
cell = (tableView.dequeueReusableCell(withIdentifier: resusableIdentifier) as? ViewTableCell)

if cell == nil {
cell = ViewTableCell(style:UITableViewCellStyle.default, reuseIdentifier: resusableIdentifier)
}

cell?.videoName.text = ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoTitle
cell?.tapAction = { (cell) in

self.onTapPlayButton(cell)

}
cell?.playButton.addTarget(self, action: #selector(TrendViewController.onTapPlayButton), for: .touchUpInside)
return cell!
}

func onTapPlayButton(_ selectedCell:ViewTableCell){

if let index = self.trendTableView.indexPath(for: selectedCell){
//put your code to execute here
let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[index.row].videoUrl, ofType:"mp4")

let path = NSURL.fileURL(withPath: url!)
currentCell = tableView.cellForRow(at: index) as! ViewTableCell
currentCell.videoPlayerItem = AVPlayerItem.init(url: path)
let playImage = UIImage(named: "image_video_play") as UIImage?
let pauseImage = UIImage(named: "image_video_pause") as UIImage?
if currentCell.avPlayer?.rate == 1.0 {
currentCell.stopPlayback()
currentCell.playButton.setImage(playImage, for: .normal)

} else {
currentCell.startPlayback()
currentCell.playButton.setImage(pauseImage, for: .normal)
}
}
}

在上面更新的代码中,当我播放视频并滚动表格 View 时,同一视频会在当前正在播放视频的单元格的第三个单元格中播放。

最佳答案

要补救这种情况,您应该做的是在 UITableViewCell 中添加一个委托(delegate)。确保协议(protocol)继承“类”,并使委托(delegate)“弱”以防止引用循环。您可以在 cellForRowAt 委托(delegate)函数中设置委托(delegate),并在单元格本身内添加/删除播放按钮。每当按下按钮时,委托(delegate)将通过传递行号、电影 ID 或您认为适合您需要的任何内容来通知包含 UITableView 的类。然后您可以打开正确的电影。

关于ios - 表格 View 单元格中的播放按钮显示错误的视频,因为它没有获得正确的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43191382/

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