2015-04-06 19 views
-1

我已經經歷了幾個小時,包括谷歌解決方案和一些教程,但我找不到它。我還是Swift和Xcode 6的新手。如何使用Swift在Xcode 6的numberOfItemsSection中傳遞多個值?

我不知道編碼有什麼問題。 (我想我知道)。

我想使用屬性列表附加到我的項目中的標籤。

import UIKit 

let reuseIdentifier = "Cell" 

class TransportCollectionView: UICollectionViewController { 


    var trans = Array<String>() 
    var labelTrans:AnyObject? 

    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) 

     // Do any additional setup after loading the view. 

     let path = NSBundle.mainBundle().pathForResource("transport", ofType: "plist") 
     let labeltrans2 = NSArray(contentsOfFile: path!) 

     labelTrans = labeltrans2! 

     trans = ["alorgajah3.png", "ayerkeroh3.png", "ayermolek3.png","batuberendam3.png", "bertamulu3.png", "batangmelaka3.png","bukitkatil3.png", "bukitrambai3.png", "jasin3.png","kemterendak3.png", "krubong3.png", "kualalinggi3.png","masjidtanah3.png", "merlimau3.png", "mitc3.png", "muar3.png","pantaikundor3.png", "payaikan3.png", "pengkalankempas3.png","pokokmangga3.png", "pulaugadong3.png", "tampin3.png", "tangkak3.png"] 

    } 

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

    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 trans.count 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell : TransportCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as TransportCell 

     cell.transImg?.image = UIImage(named: trans[indexPath.row]) 
     cell.transLabel!.text = toString(labelTrans![indexPath.row]!.objectForKey("name")!) 

     return cell 
    }  

錯誤說

fatal error: unexpectedly found nil while unwrapping an Optional value

是。我知道我的編碼沒有賦予labelTrans任何值。有人可以幫助我,如何分配多個值?

+0

你必須確認錯誤發生在哪一行這是'cell'是'nil'? 「transLabel」等於「nil」嗎?你必須確定_which_可選項在被解包時爲'nil'。 – Rob 2015-04-06 01:36:24

+0

哪一行導致錯誤?另外,當您發佈代碼時,請先清理它;我們不需要看Apple評論過的樣板或任何只能調用超級或沒有任何代碼的方法。 – rdelmar 2015-04-06 01:37:55

+0

** cell.transLabel!.text = toString(labelTrans![indexPath.row] !. objectForKey(「name」)!)**。這是導致錯誤的行@Rob – harry 2015-04-06 01:41:50

回答

0

你有兩個地方你正在展開一個可以爲零的可選項。爲了調試(或者更好,爲了避免這種崩潰),最好使用if let解包模式。

// rewrite this: 

let labeltrans2 = NSArray(contentsOfFile: path!) 

labelTrans = labeltrans2! 

// as this: 

if let labeltrans2 = NSArray(contentsOfFile: path!) { 
    labelTrans = labeltrans2 
} 

這樣,如果labeltrans2爲零,if塊內沒有任何內容被執行。如果labeltrans2不爲零,則會創建labeltrans2常量。

你應該檢查:

cell.transLabel 

它是有效的,連接的IBOutlet中

而且,這段代碼:

labelTrans![indexPath.row]!.objectForKey("name")! 

這是非常令人費解,只是因爲你定義labelTrans到類型爲AnyObject?。但它是一個NSArray,所以如果你將它定義成:

var labelTrans: NSArray<String>? 

,你可以這樣做:

if let text = labelTrans[indexPath.row] { 
     // use text 
} 

旁註:

定義let reuseIdentifier = "Cell"類的外部是沒有意義的:它在全球各地不變。你應該在裏面使用這個常數collectionView:cellForItemAtIndexPath:

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
      let reuseIdentifier = "Cell" 
      // and actually use it here 

      let cell : TransportCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier ,forIndexPath: indexPath) as TransportCell 
      ... 
相關問題