【发布时间】:2016-03-07 14:45:04
【问题描述】:
我正在尝试按照this tutorial 中的描述实现集合 vie
它在下一行崩溃
let gelleryImageView: UIImageView = (cell.viewWithTag(100) as! UIImageView)
与
fatal error: unexpectedly found nil while unwrapping an Optional value
我的 imageView 是可重用单元格的唯一孩子,并且 imageView 的标签为100
方法的其余部分
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
// Configure the cell
print(cell.subviews.count)
let gelleryImageView: UIImageView = (cell.viewWithTag(100) as! UIImageView)
gelleryImageView.image = images[indexPath.row]
return cell
}
【问题讨论】:
-
推荐你使用标签的教程(而不是告诉你为什么这是一个坏主意)是一个糟糕的教程。你是怎么检查标签是 100 的?
-
在属性检查器的界面生成器中设置
-
你说是100,我问你怎么知道是100,你在运行时检查了吗?您的错误表明它实际上不是 100。也许图像视图甚至不是子视图...
-
是的,抱歉误读了您的评论。如果有帮助,我添加了 2 个屏幕截图。
-
您需要在运行时调试和检查真相——可能您正在编辑标签或从 NIB / 故事板以外的其他东西实例化单元格
标签: ios swift uiimageview uicollectionview