繁体   English   中英

如何在UICollectionViewCell内更改UILabel的值?

[英]How do I change the value of a UILabel inside of a UICollectionViewCell?

我有一个集合视图,并且在集合视图单元格中放置了一个UILabel。

当创建将出现在视图中的每个单元格时,这些是我想分配给标签的值:

let fibonacciSeries = ["0", "1", "2", "3", "5", "8", "13", "21", "34", "55", "89", "144", "∞", "☯"]

let myDeck = Card().createDeck(fibonacciSeries)

struct Card {

    var name: String

    func createDeck(deckType: [String]) -> [Card] {
        var deck = [Card]()
        for name in deckType {
            deck.append(Card(name: name))
            }
            return deck
        }
    }

我在此函数中尝试将值分配给标签时,收到“无法将类型'String'的值转换为预期的参数类型'UIView'的错误:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CardCell

    let value = myDeck[indexPath.row]
    cell.addSubview(value.name) <----- error here
    return cell
}

我想我真的了解它在告诉我什么; 想给cell.addSubview一个UIView,但我给它value.name(一个字符串)。 我在这里找到了一些可以回答这个问题的帖子,但是它们在Objective-C中,我不了解第一件事。 例如: 如何在UICollectionViewCell上设置UILabel?

我也试过这个:

...

    let value = myDeck[indexPath.row].name <--- this didn't work
    cell.addSubview(value)
    return cell
}

非常感谢这位初学者的任何帮助。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CardCell
  var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
  label.center = CGPointMake(160, 284)
  label.textAlignment = NSTextAlignment.Center
  label.text = myDeck[indexPath.row].name as! String 
  cell.addSubview(label) 
    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM