我想使用具有合成布局的集合视图制作时间轴,其中记录按天分组。每个组包含当天并排排列的所有记录。日子之间的间隔是几天,例如每天2px。
尝试使用构图布局设计此图形,我的倾向是我需要2个组。一个具有自定义布局的主组,因为单元格之间的间隔时间使它们分开,而嵌套日组则包含所有记录。这将是水平的,因为记录以一致的间距并排放置。
这是我尝试的布局
func createLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let dayItemSize = NSCollectionLayoutSize(widthDimension: .absolute(self.cellWidth), heightDimension: .fractionalHeight(1))
let dayItem = NSCollectionLayoutItem(layoutSize: dayItemSize)
let numberOfPostsInDay = 5 // self.days[?].numberOfPosts how would I set this?
let dayItemsContentWidth = CGFloat(numberOfPostsInDay) * self.cellWidth
let dayGroupSize = NSCollectionLayoutSize(widthDimension: .absolute(10),heightDimension: .fractionalHeight(1.0))
let dayGroup = NSCollectionLayoutGroup.horizontal(layoutSize: dayGroupSize, subitems: [dayItem])
// Determine main content width
let numberOfDays = self.endDate.days(from: self.startDate)
let contentWidth = numberOfDays * CGFloat(5) // 5 pixels per day on the timeline?
let mainGroupSize = NSCollectionLayoutSize(widthDimension: .absolute(contentWidth), heightDimension: .fractionalHeight(1.0))
let mainGroup = NSCollectionLayoutGroup.custom(layoutSize: mainGroupSize) { env in
var frame = CGRect(x: 0,y: 0,width: self.cellWidth,height: self.collectionView.frame.height)
let firstDay = self.days[i].date
var items = [NSCollectionLayoutGroupCustomItem(frame: frame)]
i = 1
for day in self.days {
let dayDiff = day.date.days(from: firstDay)
frame.origin.x = self.cellWidth * CGFloat(dayDiff)
items.append(NSCollectionLayoutGroupCustomItem(frame: frame))
}
return items
}
let leftInset = layoutEnvironment.container.contentSize.width / 2 - dayItemSize.widthDimension.dimension
let rightInset = leftInset
let section = NSCollectionLayoutSection(group: mainGroup)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: leftInset, bottom: 0, trailing: rightInset)
return section
}
let config = UICollectionViewCompositionalLayoutConfiguration()
config.scrollDirection = .horizontal
layout.configuration = config
return layout
}
我正在使用字典-self.days,其中包含带日期的一天,然后是该天的帖子数。
我的问题是NSCollectionLayoutGroupCustomItemProvider闭包仅提供环境,不提供有关indexPath或其布局项的任何内容。我的印象是,布局基本上会一遍又一遍地调用此回调,直到有足够的单元格来填充该部分为止。我不确定是否可以指定哪个单元格可以进入哪个组?我需要使用这些部分吗?