Swift 确定集合视图单元格的大小
我正在尝试调整集合视图中特定单元格的大小。好的,我正试着做一个这样的布局 黑色的图像应该是一个图像。所以它应该是一个大图像,然后是两个小图像。好吧,假设我有30张图片。现在我想要0,4,6,10,12,16,18,22,24,28,30的单元格。。。这些是我想要有一个大的形象,但有麻烦的细胞。你可以看到它上升4,然后上升2,依此类推。非常感谢您的帮助 我使用UICollectionViewLayout获得了该设计。我已经用基本逻辑让perRowHeight:CGFloat=190更改了每个单元格的CGRect。在perRowHeight中,我添加了三个单元格。那就是,一个大细胞和两个小细胞 在您的设计中,有六种不同的类型。所以我将indexath.item除以6 编码 故事板 输出Swift 确定集合视图单元格的大小,swift,uicollectionview,uicollectionviewlayout,Swift,Uicollectionview,Uicollectionviewlayout,我正在尝试调整集合视图中特定单元格的大小。好的,我正试着做一个这样的布局 黑色的图像应该是一个图像。所以它应该是一个大图像,然后是两个小图像。好吧,假设我有30张图片。现在我想要0,4,6,10,12,16,18,22,24,28,30的单元格。。。这些是我想要有一个大的形象,但有麻烦的细胞。你可以看到它上升4,然后上升2,依此类推。非常感谢您的帮助 我使用UICollectionViewLayout获得了该设计。我已经用基本逻辑让perRowHeight:CGFloat=190更改了每个单元格
水平滚动还是垂直滚动?已启用分页功能?@McDonal_11垂直,在ur屏幕截图中未启用分页功能,6个单元格可见。第0个单元格更大。其余的都较小。@McDonal_11是的,但黑匣子里的应该是一个大图像。如果你有准确的屏幕截图,那么索引为4U的单元格是哪个?你能邮寄吗?
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
}
}