In my UICollectionView i have a cell. This is my cellForItemAtIndexPath
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell
let post = self.arrayOfDetails[indexPath.row]
var myUrl = post.image // post.image is image url
let fileUrl = NSURL(string: post.image)
aPlayer = AVPlayer(URL: fileUrl)
moviePlayerController.player = aPlayer
moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
moviePlayerController.view.sizeToFit()
moviePlayerController.showsPlaybackControls = true
videoCell!.addSubview(moviePlayerController.view)
return videoCell!
}
The problem is that the AVPlayerViewController does not appear on each cell. But only on the cell that is under the one at i looking at, so if i scroll down in my UICollectionView and once a new cell appear on the screen, the AVPlayerViewController is on that cell and not the cell in the middel of screen. Any suggestions what might me wrong here?
Video here: https://vid.me/e/fU5X
Erik Auranaune
You need a new aPlayer and moviePlayerController for each cell which has the video in it. Something like this should do it:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell
var aPlayer = AVPlayer()
let moviePlayerController = AVPlayerViewController()
let post = self.arrayOfDetails[indexPath.row]
var myUrl = post.image // post.image is image url
let fileUrl = NSURL(string: post.image)
aPlayer = AVPlayer(URL: fileUrl)
moviePlayerController.player = aPlayer
moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
moviePlayerController.view.sizeToFit()
moviePlayerController.showsPlaybackControls = true
videoCell!.addSubview(moviePlayerController.view)
return videoCell!
}
来源:https://stackoverflow.com/questions/33928536/avplayerviewcontroller-in-uicollectionviewcell-bug