我正嘗試創建一個自定義集合視圖。這是我第一次使用這種視圖類型,也是我第一次在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添加類的代碼 –