【发布时间】:2022-07-05 18:10:43
【问题描述】:
我正在为场景使用集合视图。我创建了一个自定义组合布局,如下所示。但是,在滚动时,单元格的第二部分之间有一个不需要的空间。它发生在不同的细胞类型中。我检查了间距或插图,但找不到原因。
构图部分:
struct UIHelper {
static func createLayouts(section: [SectionType], sectionIndex: Int) -> NSCollectionLayoutSection {
switch section[sectionIndex] {
case .sevenDaysWeather(_):
// MARK: - Item
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(70), heightDimension: .absolute(124))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// MARK: - Group
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(124))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.interItemSpacing = .fixed(12)
// MARK: - Section
let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .continuous
// MARK: - Header
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(24))
let headerKind = UICollectionView.elementKindSectionHeader
let headerElement = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: headerKind, alignment: .top)
section.boundarySupplementaryItems = [headerElement]
section.contentInsets = NSDirectionalEdgeInsets(top: 12, leading: 16, bottom: 20, trailing: 0)
return section
}
}
集合视图部分:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let type = sections[indexPath.section]
switch type {
case .sevenDaysWeather(let viewModels):
guard let sevenDaysCell = collectionView.dequeueReusableCell(withReuseIdentifier: SevenDaysCollectionViewCell.identifer, for: indexPath) as? SevenDaysCollectionViewCell else { return UICollectionViewCell()}
sevenDaysCell.configure(with: viewModels[indexPath.row])
return sevenDaysCell
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: HeaderCollectionReusableView.identifier, for: indexPath) as! HeaderCollectionReusableView
header.configure(section: sections, indexPath: indexPath)
return header
}
}
编辑:通常我在集合视图中还有两个部分。为了使示例更清晰,我修剪了这些部分。但逻辑与给定示例相同。
【问题讨论】:
-
你有一个 LOT 在那里进行。尝试从更简单的开始,并尝试让 one 部分正常工作。如果可行,请尝试添加另一个部分。如果你不能让一个部分工作,最好的办法是创建一个minimal reproducible example。使用非常简单的单元格布局和非常简单的数据,这样我们就可以看看发生了什么。
-
谢谢@DonMag。我尝试按照您的建议变得更简单,但无法弄清楚原因。我尝试制作更简单的代码供您查看。基本上,我为每个部分使用自定义的组合布局。但他们所有人的想法都是一样的。
-
我也有同样的问题。我在 IOS 15 中注意到这一点,我运行 ios 14 的设备没有此错误。不确定是什么原因造成的。如果我发现有什么不好的消息更新你,我会更深入地挖掘自己。
-
嗨@SpencerShelton,感谢您的评论。我解决了我的问题并将其发布为答案。您可以检查一下,也许它也会对您有所帮助。
标签: ios swift uicollectionview uikit uicollectionviewcompositionallayout