2015-05-08 57 views
0

我正尝试创建一个自定义集合视图。这是我第一次使用这种视图类型,也是我第一次在iOS编程几年后使用Swift。iOS Swift:在UICollectionView中展开可选错误致命错误

这里是小区的代码:

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()语句,对于数组中的每个条目,输出name的值全部6出现,所以我知道这个数组在那里不是零值。此外,我试图改变

cell.name.text = p.name 

只是

cell.name.text = "test string" 

,看看是否发挥了作用,我仍然得到了同样的错误。

+0

保持冷静。 :)说真的,这很好,但问题的根源在于别处,你需要弄清楚在哪里。使用日志记录('println')来确定什么是零。它是'cell.name'吗?它是'p'吗?它是'p.name'吗? – matt

+0

您是否使用Storyboard创建了一个'CollectionViewController'? –

+0

为自定义UICollectionViewCell添加类的代码 –

回答

1

您需要删除下面的行放在viewDidLoad()

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

如果您正在使用故事板,则不想调用此功能。它会覆盖你在故事板中的内容。

+0

这个伎俩!非常感谢。 – Danickar

+0

不客气@Danickar –

1

问题可能是根据以下情况。

var p: Administrator = principals[indexPath.row] 

如果管理员是一类,它有一个属性叫名字,如果这个属性是可选的类型,遗憾的是,如果它没有值时,可以创建问题。因此,如果这是那么情况做如下,

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

管理员类/结构可B中定义为

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

这个问题可能是由很多原因引起的,这个问题需要更多的信息来限制这个问题 –

+1

我没有说这是唯一的问题,我说这可能是原因。既然他在讲述它发生的地方,我认为这可能是解决方案之一。如果你有不同的想法,请在此分享。 – Amit89

+0

直到不​​给带来更多关于这个问题的信息,答案太广了! –

相关问题