2015-05-08 58 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

直到不​​給帶來更多關於這個問題的信息,答案太廣了! –

相關問題