如何使用Swift在Xcode 6的numberOfItemsSection中传递多个值?

哈里

我已经经历了几个小时,包括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分配任何值。有人可以帮助我,如何分配多个价值?

迭戈·弗雷尼奇(Diego Freniche)

您有两个地方要展开一个可以为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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Swift语言处理XCode中的多个目标?

如何使用Swift在Xcode 6中执行自动搜索?

如何使用swift为Xcode中的特定文本添加颜色?

如何使用Swift在Xcode中设置背景图片?

如何使用Swift在Xcode中创建自定义按钮?

如何使用Swift在Xcode中组织文件以进行iOS开发?

如何在instr中传递多个值?

Swift Xcode 6:如何在一个语句中使用多个函数?

如何使用swift在Xcode中传递JSON数组视图控制器

使用Swift的xcode 6中的iAd

ReactJS:如何在值中传递多个值?

如何使用Swift将GIF /视频添加到Xcode 6的着陆屏幕背景中?

如何使用Interface Builder和Swift在xCode6中正确创建情节提要

如何使用Swift将搜索栏与Xcode 6中的uiwebview连接?

如何使用$ _SESSION传递多个页面中的值并一起传递$ _POST

如何在单个改装请求中传递多个值

如何在通知中快速传递多个值

如何在Cookie中通过地图传递多个值

如何通过 Django 模板中的相同变量传递多个值

如何在jQuery ajax中传递多个输入形式的值?

Jmeter,如何在jmeter中传递逗号分隔的多个值

如何在FlatList extraData中传递多个对象或值

如何在XML属性中传递多个值

如何在单个prop中传递多个值以做出反应?

如何在googleapis中传递多个原始值?

如何在 PowerShell 函数中传递多个数组的值

使用 Java 在 TestNG 中传递多个可选默认值

使用多个条件从 Python 中的数组对象传递值

使用Swift和Xcode 6对UIGestureRecognizer进行子类化-如何导入UIGestureRecognizerSubclass.h?