Ios 更改带有水平和垂直滚动的自定义collectionview的显示

Ios 更改带有水平和垂直滚动的自定义collectionview的显示,ios,swift3,uicollectionview,Ios,Swift3,Uicollectionview,这里我有输出代码 CustomCollectionViewController CustomCollectionViewLayout 导入UIKit 类CustomCollectionViewLayout:UICollectionViewLayout{ 设单元高度=30.0 设单元宽度=100.0 让STATUS_BAR=UIApplication.shared.statusBarFrame.height var CellAttributedDictionary=字典() var content

这里我有输出代码

CustomCollectionViewController

CustomCollectionViewLayout

导入UIKit
类CustomCollectionViewLayout:UICollectionViewLayout{
设单元高度=30.0
设单元宽度=100.0
让STATUS_BAR=UIApplication.shared.statusBarFrame.height
var CellAttributedDictionary=字典()
var contentSize=CGSize.zero
var datasourcediddupdate=true
覆盖变量collectionViewContentSize:CGSize{
返回self.contentSize
}
重写func prepare(){
datasourcediddupdate=false
//循环浏览数据源的每个部分。
如果让sectionCount=collectionView?.numberOfSections,sectionCount>0{
对于0…sectionCount-1中的节{
//循环浏览本节中的每个项目。
如果让rowCount=collectionView?.numberOfItems(第节),则rowCount>0{
对于0中的项目…行计数-1{
//为单元格生成UICollectionVieLayoutAttribute。
让cellIndex=IndexPath(项:项,节:节)
设xPos=双倍(项目)*单元宽度
设yPos=双(截面)*单元高度
让cellAttributes=UICollectionViewLayoutAttribute(forCellWith:cellIndex)
cellAttributes.frame=CGRect(x:xPos,y:yPos,width:CELL\u width,height:CELL\u height)
//根据细胞类型确定zIndex。
如果节==0&&item==0{
cellAttributes.zIndex=4
}如果节==0,则为else{
cellAttributes.zIndex=3
}如果项==0,则为else{
cellAttributes.zIndex=2
}否则{
cellAttributes.zIndex=1
}
CellAttributesDictionary[cellIndex]=cellAttributes
}
}
}
}
让contentWidth=Double(collectionView!.numberOfItems(第0节))*单元格宽度
让contentHeight=Double(collectionView!.numberOfSections)*单元格高度
self.contentSize=CGSize(宽度:contentWidth,高度:contentHeight)
}
是否覆盖func layoutAttributesForElements(在rect:CGRect中)->[UICollectionViewLayoutAttributes]{
var attributesInRect=[UICollectionViewLayoutAttribute]()
对于CellAttributesDictionary.values中的cellAttributes{
如果矩形相交(cellAttributes.frame){
attributesInRect.append(单元格属性)
}
}
返回属性返回
}
重写func layoutAttributesForItem(在indexPath:indexPath)->UICollectionViewLayoutAttributes{
返回CellAttributedDictionary[indexPath]!
}
覆盖函数应验证布局(对于边界新边界:CGRect)->Bool{
返回真值
}    
}
问题 我希望输出的顺序相反,即

我应该在这里做什么改变。
我正在一个更大的应用程序中制作它,这只是它的一个原型。

我已经尝试了你的代码。通过在
CustomCollectionViewLayout
类中更改以下内容

 override func prepare() {


    dataSourceDidUpdate = false

    // Cycle through each section of the data source.
    if let sectionCount = collectionView?.numberOfSections, sectionCount > 0 {
        for section in 0...sectionCount-1 {

            //ADD THIS NEW LINES.
            let xPos = (Double(section) * CELL_WIDTH) + (Double(section) * 5)
           var yPos : Double = 0.0

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

                    // Build the UICollectionVieLayoutAttributes for the cell.
                    let cellIndex = IndexPath(item: item, section: section)

                    //let xPos = Double(item) * CELL_WIDTH
                    //let yPos = Double(section) * CELL_HEIGHT

                    // Comment above lines and add the below lines. 
                    yPos = (Double(item) * CELL_HEIGHT) + (Double(item) * 5)

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

                    // 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!.numberOfItems(inSection: 0)) * CELL_WIDTH
    let contentHeight = Double(collectionView!.numberOfSections) * CELL_HEIGHT
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)

}
输出


检查此项,UICollectionView滚动方向??水平/垂直?在代码中,我没有给出滚动方向,在故事板中,布局=自定义,我已经更新了答案。如果您满意,请接受并投票。我已再次更新。是否可以在每个部分上方添加标题?如果您同意您的问题答案,请对此投票。对于标题,您可以通过此链接获取我的答案。""
import UIKit

class CustomCollectionViewLayout: UICollectionViewLayout {

let CELL_HEIGHT = 30.0
let CELL_WIDTH = 100.0
let STATUS_BAR = UIApplication.shared.statusBarFrame.height


var cellAttrsDictionary = Dictionary<IndexPath, UICollectionViewLayoutAttributes>()
var contentSize = CGSize.zero

var dataSourceDidUpdate = true

override var collectionViewContentSize : CGSize {
    return self.contentSize
}

override func prepare() {
    dataSourceDidUpdate = false

    // Cycle through each section of the data source.
    if let sectionCount = collectionView?.numberOfSections, sectionCount > 0 {
        for section in 0...sectionCount-1 {

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

                    // Build the UICollectionVieLayoutAttributes for the cell.
                    let cellIndex = IndexPath(item: item, section: section)
                    let xPos = Double(item) * CELL_WIDTH
                    let yPos = Double(section) * CELL_HEIGHT

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

                    // 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!.numberOfItems(inSection: 0)) * CELL_WIDTH
    let contentHeight = Double(collectionView!.numberOfSections) * CELL_HEIGHT
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)

}

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

    var attributesInRect = [UICollectionViewLayoutAttributes]()

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

    return attributesInRect
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    return cellAttrsDictionary[indexPath]!
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    return true
}    
}
 override func prepare() {


    dataSourceDidUpdate = false

    // Cycle through each section of the data source.
    if let sectionCount = collectionView?.numberOfSections, sectionCount > 0 {
        for section in 0...sectionCount-1 {

            //ADD THIS NEW LINES.
            let xPos = (Double(section) * CELL_WIDTH) + (Double(section) * 5)
           var yPos : Double = 0.0

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

                    // Build the UICollectionVieLayoutAttributes for the cell.
                    let cellIndex = IndexPath(item: item, section: section)

                    //let xPos = Double(item) * CELL_WIDTH
                    //let yPos = Double(section) * CELL_HEIGHT

                    // Comment above lines and add the below lines. 
                    yPos = (Double(item) * CELL_HEIGHT) + (Double(item) * 5)

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

                    // 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!.numberOfItems(inSection: 0)) * CELL_WIDTH
    let contentHeight = Double(collectionView!.numberOfSections) * CELL_HEIGHT
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)

}