简体   繁体   English

如何在collectionView自定义布局中的其他补充标题单元格上方添加标题补充单元格

[英]How to add a header supplementary cell above other supplementary header cells In a collectionView custom layout

So I have a custom layout collectionView that looks something like this: 所以我有一个custom layout collectionView ,看起来像这样:

在此处输入图片说明

I did this with the help of @McDonal_11 with a custom layout [not a flowLayout ] that basically allows my collectionView to display each section with it's items in a separate column. 我在@ McDonal_11的帮助下使用了自定义布局[不是flowLayout ],该布局基本上允许我的collectionView在单独的列中显示每个section及其项目。

The thing is that I want to add a supplementary header above all these supplementary headers that is not part of the sections . 问题是我想所有这些supplementary headers supplementary header 之上添加一个supplementary header 这不是本sections一部分。

something like this: 像这样的东西:

在此处输入图片说明

How should I do so? 我应该怎么做?

Below changes you will get close to that result. 在更改下面,您将接近该结果。

For Portrait, I have Changed xPosition and Cell width for Section 0 and item 0 对于Portrait,我更改了第0部分和第0项的xPosition和像Cell width

For Landscape, I have Changed xPosition , yPosition and Cell width for Section 0 and item 0. 对于风景,我更改了第0部分和项目0的xPositionyPosition和像Cell width

I did some logic for Cell should be centre of SuperView . 我为Cell应该是SuperView中心做了一些逻辑。

override func prepare() {

    dataSourceDidUpdate = false

    if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
    {
        if let sectionCount = collectionView?.numberOfSections, sectionCount > 0 {
            for section in 0...sectionCount-1 {

                let maxNum = max(section, sectionCount + 1)
                let minNum = min(section, sectionCount + 1)

                let difference = maxNum - minNum

                var xPos = (Double(UIScreen.main.bounds.width) - (Double(difference) * CELL_WIDTH) - (Double(difference) * horizontalSpacing))

                var yPos : Double = 35.0

                if let rowCount = collectionView?.numberOfItems(inSection: section), rowCount > 0 {

                    for item in 0...rowCount-1 {


                        xPos = (Double(UIScreen.main.bounds.width) - (Double(difference) * CELL_WIDTH) - (Double(difference) * horizontalSpacing))

                        CELL_WIDTH = 100.0

                        let cellIndex = IndexPath(item: item, section: section)

                        if item == 0
                        {
                            portrait_Ypos = headerSpacing + 30
                        }
                        else
                        {
                            portrait_Ypos = portrait_Ypos + CELL_HEIGHT + verticalSpaing
                        }
                        yPos = portrait_Ypos

                        if section == 0 && item == 0
                        {
                            xPos = 20
                            yPos = 0.0
                            CELL_WIDTH = (Double(UIScreen.main.bounds.width)) - 40
                        }


                        let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex)
                        cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)


                        xPos = (Double(UIScreen.main.bounds.width) - (Double(difference) * CELL_WIDTH) - (Double(difference) * horizontalSpacing))

                        CELL_WIDTH = 100.0
                        yPos = 35.0

                        // Determine zIndex based on cell type.
                        if section == 0 && item == 0 {
                            cellAttributes.zIndex = 4
                        } else if section == 0 {
                            cellAttributes.zIndex = 3
                        } else if item == 0 {
                            cellAttributes.zIndex = 2
                        } else {
                            cellAttributes.zIndex = 1
                        }
                        cellAttrsDictionary[cellIndex] = cellAttributes

                    }

                }

            }
        }

        let contentWidth = Double(collectionView!.numberOfSections) * CELL_WIDTH + (Double(collectionView!.numberOfSections - 1) * horizontalSpacing)
        let contentHeight = Double(collectionView!.numberOfSections) * CELL_HEIGHT
        self.contentSize = CGSize(width: contentWidth, height: contentHeight)

        print("self.contentSizeself.contentSize     ", self.contentSize)
    }
    else
    {
        if let sectionCount = collectionView?.numberOfSections, sectionCount > 0 {

            for section in 0...sectionCount-1 {

                var xPos = (Double(UIScreen.main.bounds.width) - CELL_WIDTH) / 2.0

                if let rowCount = collectionView?.numberOfItems(inSection: section), rowCount > 0 {
                    for item in 0...rowCount-1 {

                        xPos = (Double(UIScreen.main.bounds.width) - CELL_WIDTH) / 2.0
                        CELL_WIDTH = 100.0

                        let cellIndex = IndexPath(item: item, section: section)

                        if section != 0
                        {
                            if item == 0
                            {
                                portrait_Ypos = portrait_Ypos + CELL_HEIGHT + headerSpacing
                            }
                            else
                            {

                                portrait_Ypos = portrait_Ypos + CELL_HEIGHT + verticalSpaing
                            }
                        }
                        else
                        {
                            if item == 0
                            {
                                portrait_Ypos = headerSpacing

                                xPos = 20
                                CELL_WIDTH = (Double(UIScreen.main.bounds.width)) - 40

                            }
                            else
                            {
                                portrait_Ypos = portrait_Ypos + CELL_HEIGHT + verticalSpaing
                            }
                        }

                        let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex)
                        cellAttributes.frame = CGRect(x: xPos, y: portrait_Ypos, width: CELL_WIDTH, height: CELL_HEIGHT)

                        xPos = (Double(UIScreen.main.bounds.width) - CELL_WIDTH) / 2.0
                        CELL_WIDTH = 100.0

                        if section == 0 && item == 0 {
                            cellAttributes.zIndex = 4
                        } else if section == 0 {
                            cellAttributes.zIndex = 3
                        } else if item == 0 {
                            cellAttributes.zIndex = 2
                        } else {
                            cellAttributes.zIndex = 1
                        }
                        cellAttrsDictionary[cellIndex] = cellAttributes

                    }

                }

            }
        }

        let contentWidth = UIScreen.main.bounds.width
        let contentHeight = CGFloat(portrait_Ypos) + CGFloat(CELL_HEIGHT)
        self.contentSize = CGSize(width: contentWidth, height: contentHeight)

        print("sPort.contentSize     ", self.contentSize)
    }



}

Dont Add header for Section 0 不要为第0部分添加标题

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

    var attributesInRect = [UICollectionViewLayoutAttributes]()

    for cellAttributes in cellAttrsDictionary.values {
        if rect.intersects(cellAttributes.frame) {

            attributesInRect.append(cellAttributes)

            let celIndPth = cellAttributes.indexPath

            if celIndPth.section != 0
            {
                if celIndPth.item == 0
                {
                    if let supplementaryAttributes = layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: cellAttributes.indexPath) {
                        attributesInRect.append(supplementaryAttributes)
                    }

                }
            }

        }
    }

    return attributesInRect
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

    return cellAttrsDictionary[indexPath]!

}

Cell For Item 单元格

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier:  "Cell", for: indexPath) as! RulesCollectionViewCell
    cell.backgroundColor = UIColor.clear

    cell.layer.borderColor = UIColor(red: 83/255, green: 50/255, blue: 129/255, alpha: 1.0).cgColor
    cell.layer.borderWidth = 1.0
    cell.txtLbl.text = values[indexPath.section][indexPath.row]
    cell.txtLbl.textAlignment = .center
    cell.txtLbl.textColor = UIColor.white


    if indexPath.section == 0 && indexPath.row == 0
    {
        cell.backgroundColor = UIColor.white
        cell.txtLbl.textColor = UIColor.red
    }


    cell.layer.cornerRadius = 5

    cell.alpha = 1.0
    return cell
}

Array Declaration 数组声明

let values = [["Main Header"],["a","b","c","d","e","f"], ["a","b","c"], ["a","b","c","d"], ["a","b","c","d","e","f"]]

Output 产量

在此处输入图片说明

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 collectionView 自定义布局中的空白部分添加补充标题视图 - How to add supplementary header view for an empty section in collectionView custom layout 如何在Objective-C中将带有补充视图的标头添加到UiCollectionView? - How to add Header with Supplementary view to UiCollectionView in Objective-C? 自定义UICollectionViewLayout和浮动辅助标题视图崩溃 - Custom UICollectionViewLayout and floating supplementary header views crash UICollectionView 和补充视图(标题) - UICollectionView and Supplementary View (header) 具有自定义布局的UICollectionView - 以编程方式添加补充视图的方式/位置? - UICollectionView with custom layout - how/where to add a supplementary view programmatically? 如何在collectionView中重新加载补充视图 - how to reload supplementary view in collectionView IGListKit - 使用补充 Header 视图添加多个部分 - IGListKit - Add Multiple Sections with a Supplementary Header View for each 具有自定义布局的UICollectionView - 无法在故事板中添加补充视图 - UICollectionView with custom layout - unable to add supplementary view in storyboard 使用自定义UICollectionViewLayout中的自动布局来调整补充视图的大小 - Sizing supplementary views with auto layout in a custom UICollectionViewLayout 如何在 UICollectionView 中正确地使补充视图的布局无效 - How to correctly invalidate layout for supplementary views in UICollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM