void ConfigureDataSource() { dataSource = new UICollectionViewDiffableDataSource <NSNumber, NSNumber> (collectionView, CellProviderHandler) { SupplementaryViewProvider = SupplementaryViewProviderHandler }; // initial data var snapshot = new NSDiffableDataSourceSnapshot <NSNumber, NSNumber> (); var idOffset = 0; var itemsPerSection = 18; foreach (var section in SectionKind.AllSections) { snapshot.AppendSections(new [] { NSNumber.FromInt32(section.EnumValue) }); var items = Enumerable.Range(idOffset, itemsPerSection).Select(i => NSNumber.FromInt32(i)).ToArray(); snapshot.AppendItems(items); idOffset += itemsPerSection; } dataSource.ApplySnapshot(snapshot, false); UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj) { // Get a cell of the desired kind. var cell = collectionView.DequeueReusableCell(TextCell.Key, indexPath) as TextCell; // Populate the cell with our item description. cell.Label.Text = $"{indexPath.Section}, {indexPath.Row}"; cell.ContentView.BackgroundColor = UIColorExtensions.CornflowerBlue; cell.ContentView.Layer.BorderColor = UIColor.Black.CGColor; cell.ContentView.Layer.BorderWidth = 1; cell.ContentView.Layer.CornerRadius = 8; cell.Label.TextAlignment = UITextAlignment.Center; cell.Label.Font = UIFont.GetPreferredFontForTextStyle(UIFontTextStyle.Title1); // Return the cell. return(cell); } UICollectionReusableView SupplementaryViewProviderHandler(UICollectionView collectionView, string kind, NSIndexPath indexPath) { var sectionKind = SectionKind.GetSectionKind(indexPath.Section); // Get a supplementary view of the desired kind. var header = collectionView.DequeueReusableSupplementaryView(new NSString(kind), TitleSupplementaryView.Key, indexPath) as TitleSupplementaryView; // Populate the view with our section's description. header.Label.Text = $".{sectionKind}"; // Return the view. return(header); } }
// +-----------------------------------------------------+ // | +---------------------------------+ +-----------+ | // | | | | | | // | | | | | | // | | | | 1 | | // | | | | | | // | | | | | | // | | | +-----------+ | // | | 0 | | // | | | +-----------+ | // | | | | | | // | | | | | | // | | | | 2 | | // | | | | | | // | | | | | | // | +---------------------------------+ +-----------+ | // +-----------------------------------------------------+ UICollectionViewLayout CreateLayout() { var config = new UICollectionViewCompositionalLayoutConfiguration { InterSectionSpacing = 20 }; return(new UICollectionViewCompositionalLayout(SectionProviderHandler, config)); NSCollectionLayoutSection SectionProviderHandler(nint sectionIndex, INSCollectionLayoutEnvironment layoutEnvironment) { var sectionKind = SectionKind.GetSectionKind(sectionIndex); var leadingItemSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(.7f), NSCollectionLayoutDimension.CreateFractionalHeight(1)); var leadingItem = NSCollectionLayoutItem.Create(leadingItemSize); leadingItem.ContentInsets = new NSDirectionalEdgeInsets(10, 10, 10, 10); var trailingItemSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1), NSCollectionLayoutDimension.CreateFractionalHeight(.3f)); var trailingItem = NSCollectionLayoutItem.Create(trailingItemSize); trailingItem.ContentInsets = new NSDirectionalEdgeInsets(10, 10, 10, 10); var trailingGroupSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(.3f), NSCollectionLayoutDimension.CreateFractionalHeight(1)); var trailingGroup = NSCollectionLayoutGroup.CreateVertical(trailingGroupSize, trailingItem, 2); var orthogonallyScrolls = sectionKind.GetOrthogonalScrollingBehavior() != UICollectionLayoutSectionOrthogonalScrollingBehavior.None; var containerGroupFractionalWidth = orthogonallyScrolls ? .85f : 1; var containerGroupSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(containerGroupFractionalWidth), NSCollectionLayoutDimension.CreateFractionalHeight(.4f)); var containerGroup = NSCollectionLayoutGroup.CreateHorizontal(containerGroupSize, leadingItem, trailingGroup); var section = NSCollectionLayoutSection.Create(containerGroup); section.OrthogonalScrollingBehavior = sectionKind.GetOrthogonalScrollingBehavior(); var sectionHeaderSize = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1), NSCollectionLayoutDimension.CreateAbsolute(44)); var sectionHeader = NSCollectionLayoutBoundarySupplementaryItem.Create(sectionHeaderSize, headerElementKind, NSRectAlignment.Top); section.BoundarySupplementaryItems = new [] { sectionHeader }; return(section); } }