一个 hacky 解决方案是覆盖您的单元格hitTest
并在点击播放器视图时取消选择:
class MyCell : UITableViewCell {
@IBOutlet private weak var videoContainer:UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Settings up the video inside the cell :
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
let player = AVPlayer(url: videoURL)
playerViewController = AVPlayerViewController()
playerViewController.player = player
player.play()
videoContainer.addSubview(playerViewController.view)
playerViewController.view.pinEdgesToSuperviewEdges()
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let convertedPoint = videoContainer.convert(point, from: self)
if let hitView = videoContainer.hitTest(convertedPoint, with: event) {
// The tap is somewhere inside the video view,
// disable user interaction on the cell and continue
self.isUserInteractionEnabled = false
return hitView
}
else {
// Tap is outside the video,
// use behavior that will trigger `didSelectItemAtIndexPath`
self.isUserInteractionEnabled = true
return super.hitTest(point, with: event)
}
}
}