Ios 集合视图和具有多个单元的扩散截面
我正在尝试使用UICollectionViewLayout和UICollectionViewDiffableDataSource创建自定义布局。 我想要一个有单元格的区域,两个单元格需要同时水平滚动。 但现在我只能看到上面的单元格,见下图 以下是我迄今为止所尝试的: 创建布局 配置数据源Ios 集合视图和具有多个单元的扩散截面,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试使用UICollectionViewLayout和UICollectionViewDiffableDataSource创建自定义布局。 我想要一个有单元格的区域,两个单元格需要同时水平滚动。 但现在我只能看到上面的单元格,见下图 以下是我迄今为止所尝试的: 创建布局 配置数据源 func configureDataSource(){ //数据源 dataSource=UICollectionViewDiffableDataSource(collectionView:collection
func configureDataSource(){
//数据源
dataSource=UICollectionViewDiffableDataSource(collectionView:collectionView){
(collectionView,indexPath,item)->UICollectionViewCell?在
guard let section=section(rawValue:indexPath.section)else{fatalError(“未知节”)}
开关段{
案例.图片:
return collectionView.dequeueConfiguredReusableCell(使用:self.configuredGridCell(),for:indepath,item:item)
案例信息:
return collectionView.dequeueConfiguredReusableCell(使用:self.configuredListCell(),for:indepath,item:item)
}
}
}
applyInitialSnapshots
func applyInitialSnapshots(){
//为我们的部分设置顺序
设sections=Section.allCases
var snapshot=NSDiffableDataSourceSnapshot()
快照.附加节(节)
dataSource.apply(快照、动画差异:false)
//最近(正交滚动条)
var imageSnapshot=nsDiffableDataSourcesectSnapshot
func createLayout() -> UICollectionViewLayout {
let sectionProvider = { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
guard let sectionKind = Section(rawValue: sectionIndex) else { return nil }
let section: NSCollectionLayoutSection
// orthogonal scrolling section of images
if sectionKind == .image {
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(self.view.frame.width), heightDimension: .fractionalHeight(0.5))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(self.view.frame.width), heightDimension: .absolute(self.view.frame.height))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .paging
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 10, trailing: 0)
// info
} else if sectionKind == .info {
section = NSCollectionLayoutSection.list(using: .init(appearance: .sidebar), layoutEnvironment: layoutEnvironment)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
} else {
fatalError("Unknown section!")
}
return section
}
return UICollectionViewCompositionalLayout(sectionProvider: sectionProvider)
}
func configureDataSource() {
// data source
dataSource = UICollectionViewDiffableDataSource<Section, Art>(collectionView: collectionView) {
(collectionView, indexPath, item) -> UICollectionViewCell? in
guard let section = Section(rawValue: indexPath.section) else { fatalError("Unknown section") }
switch section {
case .image:
return collectionView.dequeueConfiguredReusableCell(using: self.configuredGridCell(), for: indexPath, item: item)
case .info:
return collectionView.dequeueConfiguredReusableCell(using: self.configuredListCell(), for: indexPath, item: item)
}
}
}
// set the order for our sections
let sections = Section.allCases
var snapshot = NSDiffableDataSourceSnapshot<Section, Art>()
snapshot.appendSections(sections)
dataSource.apply(snapshot, animatingDifferences: false)
// recents (orthogonal scroller)
var imageSnapshot = NSDiffableDataSourceSectionSnapshot<Art>()
imageSnapshot.append(self.arts)
dataSource.apply(imageSnapshot, to: .image, animatingDifferences: false)
var allSnapshot = NSDiffableDataSourceSectionSnapshot<Art>()
allSnapshot.append(self.arts)
dataSource.apply(allSnapshot, to: .info, animatingDifferences: false)
}