【发布时间】:2019-05-18 03:48:44
【问题描述】:
我有一个stackView(称为'btnStack'),它在一个单元格(类'PresentationCell')中保存四个按钮,我试图用以下代码隐藏它:
func hideBtnStack() {
let cell = collectionView.visibleCells.first as! PresentationCell
cell.btnStack.isHidden = true
}
但是,当我运行代码时,出现以下错误:
'线程 1:致命错误:在展开可选值时意外发现 nil'
是否有更简单/更好的方式来访问单元格中的 btnStack?
cellForRowAt中的相关代码为:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! PresentationCell
}
我还在 PresentationCell 类中声明了 btnStack,如下所示:
class PresentationCell: UICollectionViewCell {
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var btnStack: UIStackView!
}
而我首先在ViewDidLoad中调用hideBtnStack方法如下:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
setUpCollectionView()
hideNavBar()
hideBtnStack()
}
提前感谢您提供的任何帮助。
【问题讨论】:
-
你确定第一个可见单元格是
PresentationCell吗?你能分享你的cellForRowAt代码吗? -
你有多少种细胞?
-
感谢您的快速回复。现在已经在上面添加了 CellforRowAt 代码。在这个presentationViewController 中只有一种单元格,即PresentationCell。
-
一种可能是你在collection view有任何数据之前调用
hideBtnStack,你也可以分享你设置数据源的位置和你调用hideBtnStack的位置吗? -
谢谢。我最初认为这也可能是一个问题。但我确保在 setUpCollectionView 以及 viewDidLoad 中的相关委托和数据源方法之后声明了 hideBtnStack。
标签: swift uicollectionviewcell stackview