使public var等于私有字符串

时间:2015-05-12 11:15:54

标签: ios string swift uicollectionviewcell

我希望公共变量等于我在通过我调用生成的方法中获得的字符串

这是我用来调用tempString

func
func handleSnapshotResult(snapshot: Dictionary<String, String>) -> Void
{
    let tempString : String = snapshot["temperature"]!
    println("TEMPERATURE: " + tempString)
}

我想在uicollectionview单元格中使用tempString,如何设置公共变量来设置CollectionViewCell.label.text = temp string

顺便说一下,我是swift的新手

2 个答案:

答案 0 :(得分:1)

您可以将此调用放在创建的位置并设置单元格的文本。当您收到来电时的回复时,您只需设置文字

即可
CollectionViewCell.label.text = "YOUR_RESPONSE_TEXT"

您可以跳过此次通话的功能。

答案 1 :(得分:0)

您可以在类定义开始的地方声明一个公共变量。所以像这样:

class MyClass {
    var temperature = ""

    [...]
}

在此之后,当您的方法返回您要设置的值时,您必须通过调用其reloadData方法来更新集合视图。

func handleSnapshotResult(snapshot: Dictionary<String, String>) -> Void {
    temperature = snapshot["temperature"]!
    collectionView.reloadData()
}

然后在cellForItemAtIndexPath(_:)方法中,您可以像这样更新标签:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
    cell.label.text = temperature

    return cell
}