gpt4 book ai didi

ios - 在 collectionViewCell 中的导出之前设置属性

转载 作者:行者123 更新时间:2023-11-29 01:47:14 26 4
gpt4 key购买 nike

我的目标是在设置 IBOutlet 之前设置 collectionViewCell 的属性。我尝试首先在 cellForRowAtIndexPath 中设置它,如下所示:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Ids.Main.PollCell, forIndexPath: indexPath) as! PollCell
let image = contentImages[indexPath.row]
let count = scores[image.associatedStack]
let isBestScore = count == bestScore
cell.associatedImage = image
cell.bestScore = isBestScore
cell.count = count != nil ? count! : 0
return cell
}

然后,当设置 socket 时,我通过 didSet 更新了他们的 UI:

@IBOutlet weak var image: DesignableImageView!{
didSet{
image.layer.cornerRadius = 35
image.layer.masksToBounds = true
if isCellSelected{
image.layer.borderColor = UIColor.blueColor()
}
}
}
@IBOutlet weak var countLabel: DesignableLabel!{
didSet{
countLabel.layer.cornerRadius = 7
countLabel.layer.masksToBounds = true
if bestScore {
countLabel.backgroundColor = UIColor.yellowColor()
}
countLabel.text = "\(count)"
countLabel.hidden = !didVote
}
}
var associatedImage: StackImage?
var bestScore = false
var didVote = false
var count = 0
var labelText = "0"
var imageIsSet = false
var isCellSelected = false

问题在于,outlet 是在属性之前设置的!这怎么可能 ?我该如何首先设置属性,然后从中设置 socket ?是因为我的单元格出队了吗?

最佳答案

如果您的单元格是在 InterfaceBuilder 中定义的,那么 socket 将始终在 awakeFromNib 中设置,它将在您的代码之前被调用。 didSet 几乎肯定不是做一半事情的正确位置。您应该将它用于在重复使用之间不会更改的 View 的初始设置。

为什么不将 didSet 添加到 bestScore 之类的内容中,然后您可以在这些点上进行适当的配置?

var bestScore = false {
didSet {
if bestScore {
countLabel.backgroundColor = .yellowColor()
} else {
countLabel.backgroundColor = .whiteColor()
}
}
}

关于ios - 在 collectionViewCell 中的导出之前设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770720/

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