【发布时间】:2016-04-09 07:20:41
【问题描述】:
我有一个集合视图,在集合视图单元格内我放置了一个 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
}
}
我在尝试将值分配给标签的此函数中收到“无法将“字符串”类型的值转换为预期的参数类型“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
}
非常感谢对这位初学者的任何帮助。
【问题讨论】:
-
cell.addSubview(value),这里的value应该是UIView,但是你的value不是。
标签: ios swift uicollectionview uicollectionviewcell