gpt4 book ai didi

swift - 在 UICollectionView sizeForItemAtIndexPath 中返回 CGSize 的奇怪错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:15 25 4
gpt4 key购买 nike

我在我的 UICollectionView 和我的 sizeForItemAtIndexPath 函数中有可变宽度,我最终返回一个 CGSize

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

var width: CGFloat = indexPath.row == 0 ? 37 : 20 // default paddings based on spacing in cell
let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightRegular)

if indexPath.row == 0 {
width += listingsFilter.stringValue.width(withConstrainedHeight: 28, font: font)
} else {
let string = viewModel.valueRangeForButton[indexPath.row - 1]
width += string.width(withConstrainedHeight: 28, font: font)
}

print(width) // some decimal e.g. 138.1239581
let w: CGFloat = width.rounded() // rounded e.g. 13

return CGSize(width: w, height: 28)
}

如果我用宽度数字(比如 128)作为返回值的 stub ,而不是使用变量,则 Collection View 遵循 UIEdgeInsets。如果我返回带有宽度变量的 CGSizeUIEdgeInsets 将被忽略,除了第一个和最后一个元素。

我觉得我发现了一些很深的 CoreGraphics 错误。

对于最后两行,如果我将它们更改为

let x: CGFloat = 128 // or 128.0, doesn't matter
return CGSize(width: x, height: 28)

还是不行。我试过使用 Int 初始值设定项返回 CGSize。我试过转换为 Ints 并返回到 CGFloats。似乎没有任何效果。

我已将问题范围缩小到这一点。它与上面的 width 代码(这是一个字符串扩展)或其他任何内容都没有任何关系。

super 奇怪。有人有这方面的经验吗?

编辑:一些图片

最上面的是 CGSize(width: w, height: 28) 其中 w 是一个 CGFloat 可以等于 128 或其他任何值值(value)。底部是 CGSize(width: 128, height: 28)

enter image description here

enter image description here

最佳答案

几个月前我遇到过类似的问题。据我所知,尝试解决此问题(如您的情况)真的很烦人,但简单得可笑。
您需要确保您的类继承自 UICollectionViewDelegateFlowLayout。它继承自 UICollectionViewDelegate。那应该可以解决问题。
让我知道这是否适合您。

关于swift - 在 UICollectionView sizeForItemAtIndexPath 中返回 CGSize 的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43287889/

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