我已经提到了这个问题
Refer to this question and answer
和提供的解决方案,当我实施它时,我观察到一个奇怪的行为。每当用户向集合视图单元格添加新项目时。它出现了,但集合视图中已经存在的其他集合视图单元格消失了。
只有在重新启动应用程序和视图后才会调用加载方法。我能够看到所有的集合视图项目。如果我尝试在不重新启动应用程序的情况下添加两个项目,它甚至会崩溃。
它在集合视图流布局中因超出范围错误而崩溃。
这是那个答案中提到的collectionviewflowlayout
`import UIKit
class HiveLayout: UICollectionViewLayout {
// Adjust this as you need
fileprivate var offset: CGFloat = 30
// Properties for configuring the layout: the number of columns and the cell padding.
fileprivate var numberOfColumns = 2
fileprivate var cellPadding: CGFloat = 10
// Cache the calculated attributes. When you call prepare(),you’ll calculate the attributes for all items and add them to the cache. You can be efficient and query the cache instead of recalculating them every time.
fileprivate var cache = [UICollectionViewLayoutAttributes]()
// Properties to store the content size.
fileprivate var contentHeight: CGFloat = 0
fileprivate var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}
// Using contentWidth and contentHeight from previous steps,calculate collectionViewContentSize.
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth,height: contentHeight)
}
override func prepare() {
// If cache is empty and the collection view exists – calculate the layout attributes
guard cache.isEmpty == true,let collectionView = collectionView else {
return
}
// xOffset: array with the x-coordinate for every column based on the column widths
// yOffset: array with the y-position for every column,Using odd-even logic to push the even cell upwards and odd cells down.
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth)
}
var column = 0
var yOffset = [CGFloat]()
for i in 0..<numberOfColumns {
yOffset.append((i % 2 == 0) ? 0 : offset)
}
for item in 0 ..< collectionView.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item,section: 0)
let columnWidths = contentWidth / CGFloat(numberOfColumns)
// Calculate insetframe that can be set to the attribute
// let cellHeight = columnWidth - (cellPadding * 2)
// let height = 250.0
let height = columnWidths * 1.5
let frame = CGRect(x: xOffset[column],y: yOffset[column],width: columnWidth,height: CGFloat(height))
let insetframe = frame.insetBy(dx: cellPadding,dy: cellPadding)
// Create an instance of UICollectionViewLayoutAttribute,sets its frame using insetframe and appends the attributes to cache.
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetframe
cache.append(attributes)
// Update the contentHeight to account for the frame of the newly calculated item. It then advances the yOffset for the current column based on the frame
contentHeight = max(contentHeight,frame.maxY)
yOffset[column] = yOffset[column] + CGFloat(height)
column = column < (numberOfColumns - 1) ? (column + 1) : 0
}
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache {
if attributes.frame.intersects(rect) {
visibleLayoutAttributes.append(attributes)
}
}
return visibleLayoutAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
}