UICollectionViewCell 错误中的 AVPlayerViewController?
AVPlayerViewController in UICollectionViewCell bug?
在我的 UICollectionView 中有一个单元格。这是我的 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!
}
问题是 AVPlayerViewController 没有出现在每个单元格上。但仅在我正在查看的单元格下方,因此,如果我在 UICollectionView 中向下滚动,并且一旦屏幕上出现一个新单元格,AVPlayerViewController 就位于该单元格上,而不是屏幕中间的单元格。有什么建议我在这里可能错了吗?
视频在这里:https://vid.me/e/fU5X
对于其中包含视频的每个单元格,您需要一个新的 aPlayer 和 moviePlayerController。应该这样做:
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!
}
在我的 UICollectionView 中有一个单元格。这是我的 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!
}
问题是 AVPlayerViewController 没有出现在每个单元格上。但仅在我正在查看的单元格下方,因此,如果我在 UICollectionView 中向下滚动,并且一旦屏幕上出现一个新单元格,AVPlayerViewController 就位于该单元格上,而不是屏幕中间的单元格。有什么建议我在这里可能错了吗?
视频在这里:https://vid.me/e/fU5X
对于其中包含视频的每个单元格,您需要一个新的 aPlayer 和 moviePlayerController。应该这样做:
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!
}