iOS Swift:在UICollectionView中解包可选的致命错误

时间:2015-05-08 15:05:41

标签: ios swift uicollectionview

我正在尝试创建自定义集合视图。这是我第一次使用这种视图类型,也是我第一次在iOS编程中使用Swift后第一次使用Swift。

以下是单元格的代码:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PrincipalViewCell

    // Configure the cell
    var p: Administrator = principals[indexPath.row]

    cell.name.text = p.name

    return cell
}

我收到以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

在线:

cell.name.text = p.name

我真的不知道为什么会这样。

编辑:

这里要求的是完整的CollectionViewController:

import UIKit

let reuseIdentifier = "PrincipalCell"
var principals = [Administrator]()

class PrincipalViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false



        // Register cell classes
       // self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool)
    {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        principals = appDelegate.admins           

        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return principals.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PrincipalViewCell

        // Configure the cell
        var p: Administrator = principals[indexPath.row]

        cell.name.text = p.name    

        return cell
    } 
}
在viewWillAppear中的

我有一个println()语句,它输出数组中每个条目的名称值,所有6个都出现了,所以我知道数组不是那里的nil值。另外,我尝试更改

cell.name.text = p.name

cell.name.text = "test string"

看看是否有所不同,我仍然遇到同样的错误。

2 个答案:

答案 0 :(得分:1)

问题可能如下所示。

var p: Administrator = principals[indexPath.row]

如果Administrator是一个类,并且它有一个名为name的属性,如果此属性是可选类型,不幸的是,如果它没有值,则可能会产生问题。因此,如果是这种情况,请按以下步骤操作,

 if let iName = p.name as? String {
        cell.name.text = iName
}

Administrator类/ struct可以定义为

class Administrator {
   var name: String! //or var name: String? 
}

答案 1 :(得分:1)

您需要删除viewDidLoad()中的以下行:

self.collectionView!.registerClass(PrincipalViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

如果您使用的是故事板,则不想打电话给它。它会覆盖故事板中的内容。