swift - 确定 Collection View 单元格的大小

标签 swift uicollectionview uicollectionviewlayout

我正在尝试调整 Collection View 中特定单元格的大小。好的,我正在尝试制作如下所示的布局。

enter image description here

黑色的图片应该是一张图片。所以它应该是 1 个大图像然后 2 个小图像。好的,假设我有 30 张图像。现在我想要位于 (0,4,6,10,12,16,18,22,24,28,30...) 的单元格,这些是我想要大图像但遇到麻烦的单元格。你可以看到它上升了 4,然后上升了 2,依此类推。将不胜感激任何帮助。 :)

最佳答案

我已经使用 UICollectionViewLayout 来获得该设计。我已经用基本逻辑 let perRowHeight : CGFloat = 190 为每个单元格更改了 CGRect。在 perRowHeight 中,我添加了三个单元格。那就是,一个大小区和两个小小区。

在您的设计中,有六种差异类型。所以我将 indexPath.item 除以 6。

编码

class ImageDesignLayout: UICollectionViewLayout {

    var CellWidth : CGFloat?
var CellHeight : CGFloat?
var cellAttrsDictionary = Dictionary<IndexPath, UICollectionViewLayoutAttributes>()
var contentSize = CGSize.zero

let perRowHeight : CGFloat = 190
var rowCountValue : Int = 0




override var collectionViewContentSize : CGSize {
    return self.contentSize
}

override func prepare() {

    var portrait_Ypos : Double = 0.0
    var xPos : Double = 0.0
    var CELL_WIDTH : Double = 0.0
    var CELL_HEIGHT : Double = 0.0
    rowCountValue = 0

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

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

                    let itemFactor = item % 6

                    print("itemFactoritemFactor   ", itemFactor)

                    switch itemFactor
                    {
                    case 0: // BIG ITEM - LEFT

                        xPos = 0
                        portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
                        CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (3/4)))


                        CELL_HEIGHT = Double(perRowHeight)
                        rowCountValue = rowCountValue + 1

                        break
                    case 1:

                        xPos = Double(Int((collectionView?.frame.size.width)! * (3/4)))
                        portrait_Ypos = portrait_Ypos + 0
                        CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
                        CELL_HEIGHT = Double(perRowHeight / 2)


                        break
                    case 2:
                        xPos = Double(Int((collectionView?.frame.size.width)! * (3/4)))
                        portrait_Ypos = portrait_Ypos + Double(perRowHeight / 2)
                        CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
                        CELL_HEIGHT = Double(perRowHeight / 2)

                        break
                    case 3:

                        xPos = 0
                        portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
                        CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (1/4)))
                        CELL_HEIGHT = Double(perRowHeight / 2)

                        break
                    case 4:

                        xPos = 0
                        portrait_Ypos = portrait_Ypos + Double(perRowHeight / 2)
                        CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (1/4)))
                        CELL_HEIGHT = Double(perRowHeight / 2)

                        break
                    case 5: // BIG ITEM - Right

                        xPos = Double(Int((collectionView?.frame.size.width)! * (1/4)))
                        portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
                        CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
                        CELL_HEIGHT = Double(perRowHeight)

                        rowCountValue = rowCountValue + 1

                        break
                    default:
                        break
                    }

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

                    cellAttrsDictionary[cellIndex] = cellAttributes
                }
            }
        }
    }

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

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

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
}

}

Storyboard

enter image description here

输出

enter image description here

关于swift - 确定 Collection View 单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981895/

相关文章:

ios - 点击按钮时显示 UIPickerView

ios - 使用滚动(手势)重新加载 CollectionView

ios - 如何通过UICollectionView访问UICollectionViewCell?

ios - UICollectionView indexPath.row 或项目,项目不稳定,跳来跳去

ios - 创建一个单元格高度大于 View 高度的水平 UICollectionView

ios - 在 SWIFT 中为 UIAlertController 设置标签?

swift - 在 Swift 中与 UIActivityViewController 共享字典

ios - UIColectionView Vertical Scrolling inside a Scrollview - 在 Collection View 区域滚动时禁用 ScrollView 的滚动

ios - Collection view Flow layout 类似于 Apple Store Application

ios - 使用 Swift 的 TableViewCell 监听器