我似乎遇到了组合布局的内存泄漏,我的组大小导致我的标题和单元格的内存使用率极高。只是为了更详细地解释我正在尝试布局一个水平网格,其中项目具有基于屏幕宽度的 1:1 比例。
在我的情况下,我想要每行 5 个项目,它们是屏幕大小的 20%,每个项目之间有 1px 间距。并且应该估计标题的高度,以便它们根据内容自行调整大小。所以下面是我到目前为止的一个片段。
// MARK: Sizing Constants
private let itemRatio: CGFloat = 0.2
var layout: UICollectionViewCompositionalLayout {
UICollectionViewCompositionalLayout { [weak self] (sectionIndex, _) -> NSCollectionLayoutSection? in
guard let self = self else { return nil }
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(self.itemRatio),
heightDimension: .fractionalWidth(self.itemRatio)))
item.contentInsets.trailing = 1
item.contentInsets.bottom = 1
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1),
heightDimension: .fractionalWidth(self.itemRatio)),
subitems: [item])
let section = NSCollectionLayoutSection(group: group)
// Enable this when finish working on the cells
section.boundarySupplementaryItems = [
.init(layoutSize: .init(widthDimension: .fractionalWidth(1),
heightDimension: .estimated(38)), // Investigate why this is causing a memory leak...
elementKind: "CollectionViewHeader",
alignment: .topLeading)
]
return section
}
}
如果我将上面的以下代码更改为下面的代码,其中我为节标题定义了一个绝对值,那么一切都会正常呈现并且没有问题。
// MARK: Sizing Constants
private let itemRatio: CGFloat = 0.2
var layout: UICollectionViewCompositionalLayout {
UICollectionViewCompositionalLayout { [weak self] (sectionIndex, _) -> NSCollectionLayoutSection? in
guard let self = self else { return nil }
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(self.itemRatio),
heightDimension: .fractionalWidth(self.itemRatio)))
item.contentInsets.trailing = 1
item.contentInsets.bottom = 1
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1),
heightDimension: .fractionalWidth(self.itemRatio)),
subitems: [item])
let section = NSCollectionLayoutSection(group: group)
// Enable this when finish working on the cells
section.boundarySupplementaryItems = [
.init(layoutSize: .init(widthDimension: .fractionalWidth(1),
// heightDimension: .estimated(38)), // Investigate why this is causing a memory leak...
heightDimension: .absolute(sectionIndex < 1 ? 120 : 72)), // Investigate why this is causing a memory leak...
elementKind: "CollectionViewHeader",
alignment: .topLeading)
]
return section
}
}
任何想法为什么会这样?