3

我为节标题创建了一个类,并将其加载到我的 UICollectionView 中。我能够显示第一个标题(尽管很奇怪,见下文),但是任何以下部分标题都是空白的。正在引用大小,但内容(背景颜色,标签)不会出现。

然后还有......确实显示的一个部分标题,它显示为缩进约。150px 没有明显的原因。标题是否默认居中对齐?如果是这样,我将如何左对齐这些?

我的部分标题类:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionView.elementKindSectionHeader:
        let section = indexPath.section
        switch section {
        case 0:
            let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TagsHeaderView", for: indexPath) as! SectionHeaderView
            tagsHeader.headerString = "Recent Tags"
            tagsHeader.backgroundColor = .green
            return tagsHeader
        default:
            let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TypeHeaderView", for: indexPath) as! SectionHeaderView
            tagsHeader.headerString = "Type"
            tagsHeader.backgroundColor = .blue
            return tagsHeader
        }
    default:
        return UICollectionReusableView()
    }
}

在我的 UICollectionView

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionView.elementKindSectionHeader:
        let section = indexPath.section
        switch section {
        case 0:
            let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TagsHeaderView", for: indexPath) as! SectionHeaderView
            tagsHeader.headerString = "Recent Tags"
            tagsHeader.backgroundColor = .green
            return tagsHeader
        default:
            let tagsHeader = searchCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "TypesHeaderView", for: indexPath) as! SectionHeaderView
            tagsHeader.headerString = "Type"
            tagsHeader.backgroundColor = .blue
            return tagsHeader
        }
    default:
        return UICollectionReusableView()
    }
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if section == 0 {
        return CGSize(width: view.frame.width, height: 18 + 22 + 6)
    } else {
        return CGSize(width: view.frame.width, height: 100)
    }
}

这就是我实例化 UICollectionView 的方式

let searchCollectionView: UICollectionView = {
    let layout = LeftAlignedCollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 6
    layout.minimumLineSpacing = 6
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.isScrollEnabled = false
    cv.register(TokenCell.self, forCellWithReuseIdentifier: "TokenCell")
    cv.register(SectionHeaderView.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TagsHeaderView")
    cv.register(SectionHeaderView.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TypesHeaderView")
    cv.backgroundColor = .white
    cv.showsVerticalScrollIndicator = false
    cv.alwaysBounceVertical = false
    cv.translatesAutoresizingMaskIntoConstraints = false
    return cv
}()

我的 CollectionViewFlowLayout

class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let attributes = super.layoutAttributesForElements(in: rect)

    var leftMargin = sectionInset.left
    var maxY: CGFloat = -1.0
    attributes?.forEach { layoutAttribute in
        if layoutAttribute.frame.origin.y >= maxY {
            leftMargin = sectionInset.left
        }

        layoutAttribute.frame.origin.x = leftMargin

        leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
        maxY = max(layoutAttribute.frame.maxY , maxY)
    }

    return attributes
}

}

这是当前结果的屏幕截图。您可以看到第一个标题出现,但缩进。第二个标题有它的空间,但它的任何内容都没有出现。在此处输入图像描述

4

1 回答 1

4

制作标签视图时遇到了同样的问题。问题出在你的CollectionViewFlowLayout.

let attributes = super.layoutAttributesForElements(in: rect)

此行获取所有属性,包括单元格、补充视图和装饰视图,这意味着您也在更改页眉和页脚的属性。

我们可以将代码更改为:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let attributes = super.layoutAttributesForElements(in: rect)

    var leftMargin = sectionInset.left
    var maxY: CGFloat = -1.0
    attributes?.forEach { layoutAttribute in
        //check if the layoutAttribute is for cell
        if layoutAttribute.representedElementCategory == .cell {
                if layoutAttribute.frame.origin.y >= maxY {
                    leftMargin = sectionInset.left
                }

                layoutAttribute.frame.origin.x = leftMargin

                leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
                maxY = max(layoutAttribute.frame.maxY , maxY)
            }
    }

    return attributes
}

为了将属性应用于单元格。然后您的页眉和页脚将正确显示。

于 2019-09-17T02:33:28.633 回答