我已经经历了几个小时,包括谷歌解决方案和一些教程,但我找不到它。我还是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