#ios #swift #uicollectionviewcompositionallayout
#iOS #swift #uicollectionviewcompositionallayout uicollectionview композиционный макет #uicollectionviewcompositionallayout
Вопрос:
Кажется, я испытываю утечку памяти с композиционным макетом, когда размер моей группы приводит к тому, что мой заголовок и ячейки используют чрезвычайно высокую память. Просто чтобы объяснить более подробно, я пытаюсь создать горизонтальную сетку, в которой элементы имеют соотношение 1: 1 в зависимости от ширины экрана.
В моем случае мне нужно 5 элементов в строке, которые составляют 20% от размера экрана с интервалом в 1 пиксель между каждым элементом. И высота заголовков должна быть оценена таким образом, чтобы они самостоятельно определяли размер на основе их содержимого. Итак, ниже приведен фрагмент того, что у меня есть до сих пор.
// 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
}
}
Есть идеи, почему это может быть так?