我使用了集合视图来显示图像集合。它正在工作,但是当我使用该功能时didDeselectItemAt
如果我单击某些图像,它会崩溃。
出现的错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
代码设置:
部分中的项目数:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellIdentifier = "ImageCell"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ImageCollectionViewCell
let imageUrls = collection?.imageUrls
// Configure the cell
// Reset alpha of first item in collection
if indexPath.row == 0 {
cell.imageView.alpha = 1.0
if videoUrl != nil {
cell.backgroundColor = UIColor.red
cell.imageView.alpha = 0.5
}
}
cell.imageView.af_setImage(withURL: URL(string: (imageUrls?[indexPath.row])!)!)
return cell
}
didDeselectItemAt:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! ImageCollectionViewCell
UIView.animate(withDuration: 0.3, animations: {
cell.imageView.alpha = 0.6
})
}
didSelectItemAt:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You've tapped me. NICE!! \(indexPath.row)")
let cell = collectionView.cellForItem(at: indexPath) as! ImageCollectionViewCell
cell.imageView.alpha = 1
let url = self.collection?.imageUrls?[indexPath.row]
self.selectedImageUrl = url
Alamofire.request(url!).responseImage(completionHandler: {
(response) in
if response.result.value != nil {
self.selectedImage.image = response.result.value
}
})
}
上面的代码可以工作,但是如果我单击某些图像,则会抛出错误。每个集合视图的第一个单元格的 alpha 值为 100% - 1.0,所有其他单元格的 alpha 值为 0.6。我注意到 - 除了集合视图的第一个单元格 - 每第 7 个单元格的 alpha 值也为 100%,如果集合视图包含 videoUrl,它将具有红色背景和 50% 的 alpha 值。
这是因为像 UITableViewController 这样的可重用单元,您在其中使用dequeueReusableCell
你是否也应该在内部使用该函数didDeselectItemAt
或者还有其他没有正确实施的事情吗?
因此,只有集合视图中的第一个单元格的 alpha 值应为 100,其他所有单元格的 alpha 值应为 60%。如果对象具有 videoUrl,则第一个元素的 alpha 值为 50%,背景为红色。
如果还有任何问题,请告诉我。提前致谢,祝您度过一个美好的夜晚!
UPDATE:
当我单击集合视图的第一个图像和第二个图像时,以下打印行将输出以下代码。这不会使应用程序崩溃,它仍然可以工作
print("OUTPUT \(String(describing: collectionView.cellForItem(at: indexPath)))")
上面的代码片段我已经放置在两个中didSelectItemAt
and didDeselectItemAt
:
didSelectItemAt 输出:
OUTPUT Optional(<CollectionViewApp.ImageCollectionViewCell: 0x7ff61679c1a0; baseClass = UICollectionViewCell; frame = (150 0; 150 100); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6080000347c0>>)
didDeselectItemAt 输出:
输出 可选(>)
当我单击集合视图中的第一张或第二张图像,然后单击最后一张图像时,将发生错误并使我的应用程序崩溃。触摸第一张图像会给我输出:
OUTPUT Optional(<CollectionViewApp.ImageCollectionViewCell: 0x7fc3cb038540; baseClass = UICollectionViewCell; frame = (150 0; 150 100); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x610000225980>>)
最后一项 - 这将使我的应用程序崩溃 - 将抛出以下输出:OUTPUT Optional(<CollectionViewApp.ImageCollectionViewCell: 0x7fc3cb03afb0; baseClass = UICollectionViewCell; frame = (300 0; 150 100); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6100002264c0>>)
.
因此,当您单击集合视图中的第一个图像之一,然后单击集合视图中的最后一个图像时,当第一个图像超出画布时,可能会触发崩溃。我用了一个水平的集合视图。