gpt4 book ai didi

ios - UICollectionView cellForItem(在 :) crashes

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

我正在使用 Swift4 和 Xcode9。

我使用下面的委托(delegate)方法创建我的 collectionView 单元格;

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mediaItemCell", for: indexPath) as! MediaItemCollectionViewCell
cell.mediaItemCellImageView.image = mediaPlayer?.item(at: indexPath.row)?.image

return cell
}

然后,我想用“停止”图像替换所选单元格的图像。为此,我尝试使用 didSelectItemAt 方法;

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if (mediaPlayer?.isPlaying == true) {
// stop the player
mediaPlayer?.stopPlayingMedia()
} else {
// change the image and start playing
let cell = collectionView.cellForItem(at: indexPath) as! MediaItemCollectionViewCell // CRASH
cell.mediaItemCellImageView.image = UIImage(named: "stop.png")

...
...
}
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
// restore the original image
let cell = collectionView.cellForItem(at: indexPath) as! MediaItemCollectionViewCell
cell.mediaItemCellImageView.image = mediaPlayer?.item(at: indexPath.row)?.image
// stop playing
mediaPlayer?.stopPlayingMedia()
}

现在,当我选择已被选中的单元格时(因此原始图像已被“stop.png”替换),上面的代码有效;即恢复原始图像,这是通过 stopPlayingMedia() 方法完成的。

当我选择与当前所选单元格不同的单元格时,应用程序崩溃。我试图将 didSelectItemAt 调用移出委托(delegate)方法,但没有成功。

令人惊讶的是,逻辑检查 if collectionView.cellForItem(at: indexPath) is MediaItemCollectionViewCell 在我选择当前选定的单元格时成功,而在我选择另一个单元格时失败。

我想更改单元格的图像,因此需要转换变量类型但失败了。为什么当我选择与当前所选单元格不同的单元格时转换失败?

谢谢

编辑:

我使用 collectionView.reloadData() 将所有图像恢复为原始图像(我在 OP 中提到了这一点 - 当调用 stopPlayingMedia() 时)。看起来,如果我将 private var index:IndexItem? 添加到我的 ViewController 类并在 didSelectItemAt 中更新它的值(以保存最后一个选定的单元格)并使用下面的代码,代码不会崩溃

extension ViewController:MediaPlayerControllerDelegate {
// this delegate method is invoked when stopPlayingMedia() is called
func mediaPlayerControllerDidFinishPlaying() {
// restore the UI
// collectionView.reloadData() -> this causes the crash, instead, I only restore one cell
let cell = collectionView.cellForItem(at: index!) as! MediaItemCollectionViewCell
cell.mediaItemCellImageView.image = mediaPlayer?.item(at: (index?.row)!)?.image

timeLabel.text = "00:00"
progressBar.progress = 0.0
}
}

最佳答案

我尝试了以下方法来创建单元格并在选择时更改单元格内的图像。一点都不崩溃。请检查实现情况,它可能对您有所帮助。

import UIKit

class ViewController: UIViewController {
private var isPlayingModel = [false, false, false, false, false] // initially all are stop, not playing

@IBOutlet weak var collectionView: UICollectionView! {
didSet{
collectionView.delegate = self
collectionView.dataSource = self
}
}
}

extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isPlayingModel.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mediaItemCell", for: indexPath) as! MediaItemCollectionViewCell
cell.imageVw.image = isPlayingModel[indexPath.row] ? #imageLiteral(resourceName: "start") : #imageLiteral(resourceName: "stop")
return cell
}
}

extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

isPlayingModel[indexPath.row] = !isPlayingModel[indexPath.row]
let cell = collectionView.cellForItem(at: indexPath) as! MediaItemCollectionViewCell
cell.imageVw.image = isPlayingModel[indexPath.row] ? #imageLiteral(resourceName: "start") : #imageLiteral(resourceName: "stop")
}
}

extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.size.width, height: 60)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 4
}
}

class MediaItemCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageVw: UIImageView!
}

Output

Check the implementation here.

关于ios - UICollectionView cellForItem(在 :) crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48440503/

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