我已经经历了几个小时,包括google解决方案和一些教程,但是我找不到它。我还是Swift和Xcode 6的新手。
我不知道编码有什么问题。(我想我确实知道)。
我想使用属性列表来附加到项目中的标签。
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
}
错误说
致命错误:解开Optional值时意外发现nil
是的。我知道我的编码没有为labelTrans分配任何值。有人可以帮助我,如何分配多个价值?
您有两个地方要展开一个可以为nil的可选内容。为了进行调试(或者更好地避免此类崩溃),最好只使用if let
展开模式。
// rewrite this:
let labeltrans2 = NSArray(contentsOfFile: path!)
labelTrans = labeltrans2!
// as this:
if let labeltrans2 = NSArray(contentsOfFile: path!) {
labelTrans = labeltrans2
}
这样,如果labeltrans2为nil,则if块内的任何内容都不会执行。如果labeltrans2不为nil,则创建labeltrans2常量。
您应该检查一下:
cell.transLabel
这是有效的,已连接的IBOutlet
另外,这段代码:
labelTrans![indexPath.row]!.objectForKey("name")!
这非常令人费解,只是因为您将labelTrans定义为type 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
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句