[英]Swift: Best way to handle unique/variable cell spacing in UICollectionView?
更新:感谢@Larme 的评论,我很轻松地做到了这一点:
func getLayout() -> UICollectionViewCompositionalLayout {
let size = NSCollectionLayoutSize.init(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(50))
let item = NSCollectionLayoutItem.init(layoutSize: size)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item])
let section = NSCollectionLayoutSection.init(group: group)
section.interGroupSpacing = 2.0 // group spacing
let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 16 // section spacing
return UICollectionViewCompositionalLayout.init(section: section, configuration: config)
}
然后,它就像拥有一个 function 一样简单,它决定何时将 append 到上一节与初始化一个新节。 这是我的一个例子:
func addMessages(_ messages:[Message]) {
var snap = self.data.snapshot()
for i in messages {
// If prev and current message has same origin, use prev section
if snap.sectionIdentifiers.last?.origin == i.origin {
snap.appendItems([i], toSection: snap.sectionIdentifiers.last!)
// Else create a new section
} else {
snap.appendSections([Section(origin: i.origin)])
snap.appendItems([i], toSection: snap.sectionIdentifiers.last!)
}
}
self.data.apply(snap)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.