我已經經歷了幾個小時,包括谷歌解決方案和一些教程,但我找不到它。我還是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任何值。有人可以幫助我,如何分配多個值?
你必須確認錯誤發生在哪一行這是'cell'是'nil'? 「transLabel」等於「nil」嗎?你必須確定_which_可選項在被解包時爲'nil'。 – Rob 2015-04-06 01:36:24
哪一行導致錯誤?另外,當您發佈代碼時,請先清理它;我們不需要看Apple評論過的樣板或任何只能調用超級或沒有任何代碼的方法。 – rdelmar 2015-04-06 01:37:55
** cell.transLabel!.text = toString(labelTrans![indexPath.row] !. objectForKey(「name」)!)**。這是導致錯誤的行@Rob – harry 2015-04-06 01:41:50