Ios UICollectionViewCell cellSize不';最后一项不工作

Ios UICollectionViewCell cellSize不';最后一项不工作,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我正在尝试复制AppStore今天的卡片视图布局 这是我的密码 extension TestViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } func collectionView(_ collectionView:

我正在尝试复制AppStore今天的卡片视图布局

这是我的密码

extension TestViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let genericCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

    if let label = genericCell.viewWithTag(100) as? UILabel {
        label.text = "\(indexPath.item)"
    }
    return genericCell
}
}

extension TestViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 40
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellHeight: CGFloat = 100

    let numberOfItemsInRow = 2
    let compressedWidth = collectionView.bounds.width / 2
    let expandedWidth = compressedWidth * 0.5
    let rowNumber = indexPath.item / numberOfItemsInRow
    let isEvenRow = rowNumber % 2 == 0
    let isFirstItem = indexPath.item % numberOfItemsInRow == 0

    var width: CGFloat = 0.0
    if isEvenRow {
        width = isFirstItem ? expandedWidth : compressedWidth
    } else {
        width = isFirstItem ? compressedWidth : expandedWidth
    }
    return CGSize(width: width, height: cellHeight)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)
}
}
对于奇数个数据源,上述代码可以正常工作。如果是偶数,则项目的最后一行之间不会有任何白色间距

我用来解决这个问题的方法是添加额外的1个单元格,自动调整宽度大小,但高度为0

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4 + 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellHeight: CGFloat = indexPath.item == collectionView.numberOfItems(inSection: 0) - 1 ? 0 : 100 
}
然而,我不认为这是最好的使用方法。有人能指导我做什么吗?
我错过了什么?谢谢

您最好的方法是使用自定义UICollectionViewFlowLayout

class TestCollectionViewLayout: UICollectionViewFlowLayout {

    var needUpdate: Bool = false

    fileprivate var cache = [UICollectionViewLayoutAttributes]()

    fileprivate var contentHeight: CGFloat = 0

    fileprivate var contentWidth: CGFloat { return collectionView?.frame.width ?? 0.0 }

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

    override func invalidateLayout() {
        needUpdate = true
        super.invalidateLayout()
    }

    override func prepare() {

        guard let collectionView = collectionView else { return }

        guard cache.isEmpty || needUpdate else {
            return
        }

        needUpdate = false

        cache = []

        contentHeight = 0

        let interitemSpacing: CGFloat = resolveMinimumInteritemSpacingForSectionAt(0)

        var yOffset: CGFloat = 0.0

        var previousItemSize: CGSize!

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

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

            let itemSize = resolveSizeForItem(at: indexPath)

            var point: CGPoint

            let isEvenColumn = item % 2 == 0

            if isEvenColumn {
                point = CGPoint(x: 0.0, y: yOffset + interitemSpacing)
            } else {
                point = CGPoint(x: previousItemSize.width + interitemSpacing, y: yOffset + interitemSpacing)
            }

            let frame = CGRect(origin: point, size: itemSize)

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

            contentHeight = max(contentHeight, frame.maxY)

            previousItemSize = frame.size

            if !isEvenColumn {
                yOffset = frame.maxY
            }
        }
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
        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]
    }

}

extension TestCollectionViewLayout {

    func resolveCollectionView<T>(_ closure: (UICollectionView) -> T?, defaultValue: T) -> T {
        if let collectionView = collectionView {
            return closure(collectionView) ?? defaultValue
        } else {
            return defaultValue
        }
    }

    func resolveSizeForItem(at indexPath: IndexPath) -> CGSize {
        let size = resolveCollectionView({ collectionView -> CGSize? in
            return (collectionView.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(collectionView,
                                                                                                   layout: self,
                                                                                                   sizeForItemAt: indexPath)
        }, defaultValue: CGSize(width: 150.0, height: 100.0))

        return size
    }

    func resolveMinimumInteritemSpacingForSectionAt(_ section: Int) -> CGFloat {
        let space = resolveCollectionView({ collectionView -> CGFloat? in
            return (collectionView.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(collectionView,
                                                                                                   layout: self,
                                                                                                   minimumInteritemSpacingForSectionAt: section)
        }, defaultValue: 10.0)

        return space
    }
}
extension TestViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellHeight: CGFloat = 100

    switch indexPath.item {
    case 0:
        return CGSize(width: (collectionView.bounds.width - 10) * 0.25, height: cellHeight)
    case 1:
    return CGSize(width: (collectionView.bounds.width - 10) * 0.75, height: cellHeight)
    case 2:
    return CGSize(width: (collectionView.bounds.width - 10) * 0.4, height: cellHeight)
    case 3:
    return CGSize(width: (collectionView.bounds.width - 10) * 0.6, height: cellHeight)
    default:
        return CGSize.zero
    }
    }
}
像使用常规布局一样使用此布局

let layout = TestCollectionViewLayout()
layout.scrollDirection = .vertical
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self

您使用的是分区的最小MuminitemSpacing为零。试着改变一下。如果我加上那一行,它会弄乱整个布局,因为正如你所看到的,第一行没有任何问题。我错过什么了吗?