gpt4 book ai didi

ios - UICollectionViewCell 的 alpha 属性在重新加载后才生效?

转载 作者:搜寻专家 更新时间:2023-10-30 21:57:51 24 4
gpt4 key购买 nike

如果我在 cellForItemAtIndexPath 中设置单元格的 alpha 属性如下...

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CGPathCell", forIndexPath: indexPath) as UICollectionViewCell
// Prepare cell
cell.alpha = 0.5
cell.setNeedsDisplay()
return cell
}

...最初出现的单元格是不透明的(alpha 为 1.0)。如果我滚动,出现的"new"(重复使用)单元格具有预期的 alpha 值。如果我滚动回开头,“原始”单元格(开始时不透明)现在也具有预期的 alpha 值。

为什么初始加载时没有设置alpha值?

无效的额外尝试:

  1. 将此添加到委托(delegate)中,我得到相同的结果:

    func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    cell.alpha = 0.5
    }
  2. 将此添加到 cellForItemAtIndexPath:

    let attrs = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
    attrs.alpha = 0.5
    cell.applyLayoutAttributes(attrs)

最佳答案

我相信这是因为 Apple 正在对作为 UICollectionView 一部分的单元格进行一些特殊的内部处理。如果您在单元格的 contentView 属性上设置 alpha,alpha 将按预期持续存在。

UICollectionViewCell docs 有点暗示:

To configure the appearance of your cell, add the views needed to present the data item’s content as subviews to the view in the contentView property. Do not directly add subviews to the cell itself.

如果您有一个 backgroundViewselectedBackgroundView,您需要分别设置它们的 alpha。

关于ios - UICollectionViewCell 的 alpha 属性在重新加载后才生效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29016096/

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