gpt4 book ai didi

ios - swift imageView、collectionView,在解包可选值时意外发现 nil

转载 作者:行者123 更新时间:2023-11-30 13:53:35 26 4
gpt4 key购买 nike

首先:我知道关于这个主题有数以千计的问题,并且肯定在其中一个问题中我的问题得到了解答。但经过几个小时的搜索,我没有找到解决我问题的答案......

我有一个 CollectionViewController (在容器内)和一个 CollectionViewCell。 Cell 内部有一个 imageView。必须使用 CollectionViewController 中的按钮更改 imageView 的图像。

这是我的代码:

class essenCell: UICollectionViewCell {
@IBOutlet weak var Bild: UIImageView!
}

(细胞)

class essenCollectionViewController: UICollectionViewController {
[...]
@IBAction func xyz(sender: UIButton!) {
//following: the line with the error (unexpectedly found nil while unwrapping an Optional value)
essenCell().Bild.image = UIImage(named: "erster")
}
}

( Collection View Controller )

首先,我认为我可以将按钮连接到单元和 Controller ,这在理论上可行,但在我的具体情况下不起作用。图像更改发生在引用parentViewController 的if-Loop(在我的Controller 内)内。因为我无法在单元格内使用此引用,所以我需要将按钮功能放在 Controller 中。

帮助我!

最佳答案

collectionViewCell 的所有设置都应在 cellForItemAtIndexPath 中完成。

var buttonPressed = false

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("reuseCell", forIndexPath: indexPath) as! essenCell

if buttonPressed {
cell.Bild.image = UIImage(named: "erster")
} else {
cell.Bild.image = UIImage(named: "placeHolder")

return cell
}

@IBAction func xyz(sender: UIButton!) {
buttonPressed = true
collectionView?.reloadData()
}

关于ios - swift imageView、collectionView,在解包可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944535/

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