gpt4 book ai didi

ios - 如何更改 UICollectionViewCell 中 UILabel 的值?

转载 作者:行者123 更新时间:2023-11-29 01:05:02 27 4
gpt4 key购买 nike

我有一个 Collection View ,在 Collection View 单元格中我放置了一个 UILabel。

这些是我要在创建将出现在 View 中的每个单元格时分配给标签的值:

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 中,我首先不知道。例如:How to set UILabel on UICollectionViewCell?

我也试过这个:

...

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
}

关于ios - 如何更改 UICollectionViewCell 中 UILabel 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com