我正尝试创建一个自定义集合视图。这是我第一次使用这种视图类型,也是我第一次在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"
,看看是否发挥了作用,我仍然得到了同样的错误。
保持冷静。 :)说真的,这很好,但问题的根源在于别处,你需要弄清楚在哪里。使用日志记录('println')来确定什么是零。它是'cell.name'吗?它是'p'吗?它是'p.name'吗? – matt
您是否使用Storyboard创建了一个'CollectionViewController'? –
为自定义UICollectionViewCell添加类的代码 –