gpt4 book ai didi

ios - UICollectionView didDeselectItemAtIndexPath 影响多个单元格并给出 "nil"

转载 作者:行者123 更新时间:2023-11-30 13:32:47 24 4
gpt4 key购买 nike

我有一个包含 11 个单元格的水平 Collection View ,当选择一个单元格时,其边框会变为绿色。我遇到的问题是,当我选择单元格时,它会更改不可见单元格的边框。

此外,当我选择最后一个单元格时,应用程序崩溃,给出“ fatal error :展开可选值时意外发现 nil”,引用 didDeselectItemAtIndexPath

可能是什么问题?

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return frameArray.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let frameCell = collectionView.dequeueReusableCellWithReuseIdentifier("frameCell", forIndexPath: indexPath) as! FrameViewCell

dispatch_async(dispatch_get_main_queue()) {
frameCell.imageView.image = UIImage(named: self.frameArray[indexPath.row])
}

return frameCell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

dispatch_async(dispatch_get_main_queue()) {
let frameCell = collectionView.cellForItemAtIndexPath(indexPath) as! FrameViewCell

frameCell.layer.borderWidth = 2
frameCell.layer.borderColor = UIColor.greenColor().CGColor
frameCell.imageView.alpha = 1.0

self.globalFrameIndex = self.frameArray[indexPath.row]
self.framesImageViews(self.globalFrameIndex, image: self.globalImage)


print(self.frameArray[indexPath.row])
}
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

dispatch_async(dispatch_get_main_queue()) {
let frameCell = collectionView.cellForItemAtIndexPath(indexPath) as! FrameViewCell

frameCell.layer.borderWidth = 1
frameCell.layer.borderColor = UIColor.whiteColor().CGColor
frameCell.imageView.alpha = 0.5
}

}

}

最佳答案

更改您的函数以执行一些 if-let 可选检查:

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

dispatch_async(dispatch_get_main_queue()) {
if let frameCell = collectionView.cellForItemAtIndexPath(indexPath) as? FrameViewCell
{
frameCell.layer.borderWidth = 1
frameCell.layer.borderColor = UIColor.whiteColor().CGColor
frameCell.imageView.alpha = 0.5
}
}
}

更多信息可见in this related question .

关于ios - UICollectionView didDeselectItemAtIndexPath 影响多个单元格并给出 "nil",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390604/

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