2015-04-06 20 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 
      ... 
相关问题