gpt4 book ai didi

ios - 如何在 UICollectionView 中设置 UICollectionViewCell 的垂直顶部对齐方式。 ( Objective-C )

转载 作者:行者123 更新时间:2023-12-05 00:22:56 33 4
gpt4 key购买 nike

我有 UICollectionViewCells不同的高度。我想在 UICollectionView 中设置每个单元格的顶部对齐方式.以下是附件:

enter image description here

我要UICollectionViewCells像:

enter image description here

最佳答案

您可能要求像在 pinterest 中那样的布局。
查看此教程 uicollectionview-custom-layout-tutorial-pinterest
这是您需要的代码


protocol PinterestLayoutDelegate: AnyObject {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat
}

class PinterestLayout: UICollectionViewLayout {
// 1
weak var delegate: PinterestLayoutDelegate?

// 2
private let numberOfColumns = 2
private let cellPadding: CGFloat = 10

// 3
private var cache: [UICollectionViewLayoutAttributes] = []

// 4
private var contentHeight: CGFloat = 0

private var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}

// 5
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}

override func prepare() {
// 1
guard cache.isEmpty == true, let collectionView = collectionView
else { return }

// 2
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset: [CGFloat] = []
for column in 0..<numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth)
}
var column = 0
var yOffset: [CGFloat] = .init(repeating: 0, count: numberOfColumns)

// 3
for item in 0..<collectionView.numberOfItems(inSection: 0) {

let indexPath = IndexPath(item: item, section: 0)

// 4
let photoHeight = delegate?.collectionView(
collectionView, heightForPhotoAtIndexPath: indexPath
) ?? 180

let height = cellPadding * 2 + photoHeight

let frame = CGRect(
x: xOffset[column],
y: yOffset[column],
width: columnWidth,
height: height
)

let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)

// 5
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetFrame
cache.append(attributes)

// 6
contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height

column = column < (numberOfColumns - 1) ? (column + 1) : 0
}
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var visibleLayoutAttributes: [UICollectionViewLayoutAttributes] = []

// Loop through the cache and look for items in the rect
for attributes in cache {
if attributes.frame.intersects(rect) {
visibleLayoutAttributes.append(attributes)
}
}
return visibleLayoutAttributes
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
}

使用方法:
  • 设置您的 collectionView 布局

  • let layout = PinterestLayout()
    layout.delegate = self
    collectionView.collectionViewLayout = layout
  • 实现委托(delegate)以获取单元格的高度
  • 更改 PinterestLayout.numberOfColumns并设置 colls 的数量

  • Refer to the tuto for more explanation

    关于ios - 如何在 UICollectionView 中设置 UICollectionViewCell 的垂直顶部对齐方式。 ( Objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034103/

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