我正试图制作具有特定布局的“收藏夹视图”,如下图所示
因此,某些单元格是两行两列,而有些单元格是一一对应
如果有人有一些教程或提示,请分享。
编辑-这是我的“自定义布局”类的代码
protocol CustomLayoutDelegate: class {
func collectionView(_ collectionView: UICollectionView, sizeForCellAtIndexPath indexPath:IndexPath) -> CGSize
}
class CustomLayout: UICollectionViewFlowLayout {
weak var delegate: CustomLayoutDelegate!
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
var cnt: CGFloat = 0
var cols: CGFloat = 0
var rows: CGFloat = 0
layoutAttributes?.forEach({ (attributes) in
guard let cv = collectionView else { return }
let indexPath = IndexPath(item: attributes.indexPath.item, section: 0)
let cellSize = delegate.collectionView(cv, sizeForCellAtIndexPath: indexPath)
attributes.frame = CGRect(x: cellSize.width * (cols), y: cellSize.height * rows, width: cellSize.width, height: cellSize.height)
print(cnt, cols, rows)
cnt += 1
cols = cols < 2 ? (cols + 1) : 0
if Int(cnt.truncatingRemainder(dividingBy: 3)) == 0 {
rows += 1
}
})
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
谢谢