Ios Swift Collection视图中最奇怪的错误
我面临着使用Swift遇到的最奇怪的问题,我正在开发一个游戏,我正在使用collection view单元格显示我将拥有的不同附加组件,比如衣服和其他东西,但是,我正在加载每个collectionviewcell将在字符串列表中拥有的图像名称,当我在一个单元格中单击时,希望在选定的“对象”内获得一个检查图像,如果我在另一个单元格中单击,则首先取消选择所有单元格,检查结果就会出现问题,一切正常!但是,当我在字符串列表中放置了8个以上的项时,会出现以下错误“致命错误:在展开可选值时意外发现nil”这很奇怪,因为这些项在集合视图中显示得很好,但是当我单击其中一个项时会出现问题,我重复了8个以上的项 这是我的视图控制器:Ios Swift Collection视图中最奇怪的错误,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我面临着使用Swift遇到的最奇怪的问题,我正在开发一个游戏,我正在使用collection view单元格显示我将拥有的不同附加组件,比如衣服和其他东西,但是,我正在加载每个collectionviewcell将在字符串列表中拥有的图像名称,当我在一个单元格中单击时,希望在选定的“对象”内获得一个检查图像,如果我在另一个单元格中单击,则首先取消选择所有单元格,检查结果就会出现问题,一切正常!但是,当我在字符串列表中放置了8个以上的项时,会出现以下错误“致命错误:在展开可选值时意外发现nil”这
//
// WeaponSelectorViewController.swift
// swiftSpriteAwesomeProject
//
// Created by Mauri C. on 3/2/15.
// Copyright (c) 2015 WANT. All rights reserved.
//
import UIKit
class WeaponSelectorViewController: UIViewController {
var images = [NSString]()
let defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
images = ["chancla", "chancle", "chancle2", "chancle3", "chancle4", "chancle5", "chancle7"]
images.append("chancla")
images.append("chancla")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as WeaponCollectionViewCell
cell.checkImageView.hidden = true
if(defaults.stringForKey("chancla") != nil){
if(defaults.stringForKey("chancla")! == images[indexPath.row]){
cell.checkImageView.hidden = false
cell.checkImageView.image = UIImage(named: "check")
}
}
cell.imageView.image = UIImage(named:images[indexPath.row])
cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit
return cell
}
// Uncomment this method to specify if the specified item should be selected
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
NSLog("\(indexPath)")
let cell = collectionView.cellForItemAtIndexPath(indexPath) as WeaponCollectionViewCell
NSLog("\(self.images.count)")
for var i = 0; i < self.images.count; i++ {
var celli = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: i, inSection: 0)) as WeaponCollectionViewCell
celli.checkImageView.hidden = true
}
if(cell.checkImageView.hidden){
cell.checkImageView.hidden = false
cell.checkImageView.image = UIImage(named: "check")
defaults.setObject(images[indexPath.row], forKey: "chancla")
} else {
cell.checkImageView.hidden = true
}
return true
}
}
//
//武器选择器或视图控制器.swift
//SwiftsPriteaweesome项目
//
//由Mauri C.于2015年3月2日创建。
//版权所有(c)2015。版权所有。
//
导入UIKit
类武器选择器或视图控制器:UIViewController{
var images=[NSString]()
让defaults=NSUserDefaults.standardUserDefaults()
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
图像=[“圣坛”、“圣坛”、“圣坛2”、“圣坛3”、“圣坛4”、“圣坛5”、“圣坛7”]
图像。附加(“chancla”)
图像。附加(“chancla”)
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回图像数
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
var cell=collectionView.dequeueReusableCellWithReuseIdentifier(“CollectionViewCell”,forIndexPath:indexPath)作为武器CollectionViewCell
cell.checkImageView.hidden=true
if(默认值:stringForKey(“chancla”)!=nil){
if(defaults.stringForKey(“chancla”)!==images[indexPath.row]){
cell.checkImageView.hidden=false
cell.checkImageView.image=UIImage(名为“检查”)
}
}
cell.imageView.image=UIImage(名称:images[indexPath.row])
cell.imageView.contentMode=UIViewContentMode.ScaleSpectFit
返回单元
}
//取消对此方法的注释以指定是否应选择指定的项
func collectionView(collectionView:UICollectionView,shouldSelectItemAtIndexPath indexPath:NSIndexPath)->Bool{
NSLog(“\(indexPath)”)
将cell=collectionView.cellForItemAtIndexPath(indexPath)设为WeaponCollectionViewCell
NSLog(“\(self.images.count)”)
对于变量i=0;i
请让您的问题更具可读性。哪一行导致了错误非常感谢您的快速回复,我想在集合视图的选定单元格上显示一个“检查”图像,如您所见,我从名为“图像”的字符串列表中填充其图像名称,以仅“检查”一项,我先在shouldSelectItemAtIndexPath方法上“取消”每个单元格,问题出现在我用来“取消选中”的for循环中的这一行“var celli=collectionView.cellForItemAtIndexPath(nsindepath(forItem:i,instation:0))作为WeaponCollectionViewCell”,这真的很奇怪,因为字符串列表中有8项,它工作正常!但是没有更多请使用反引号(``)来包装任何代码
,以使其可读感谢您的快速回复,我想在集合视图的选定单元格上显示一个“检查”图像,我将从您可以看到的名为“images”的字符串列表中填充其图像名称,以仅“检查”一项,即im“unkeking”在shouldSelectItemAtIndexPath方法中,每个单元格的第一个单元格,问题出现在这一行var celli=collectionView.cellForItemAtIndexPath(NSIndexPath(forItem:i,inSection:0))作为我用来“取消选中”的for循环中的WeaponCollectionViewCell
,这真的很奇怪,因为字符串列表中有8个项目,它可以正常工作!但是没有,因为你要的是没有显示的单元格,所以你让它把没有显示的单元格排出来,把事情搞得一团糟。此外,collectionView(collectionView:shouldSelectItemAtIndexPath:)
不应用于编辑项目,而仅用于确定项目是否可以选择,您可以使用collectionView(collectionView:didSelectItemAtIndexPath:)
来执行响应选择的操作。