Last active
February 17, 2019 18:41
-
-
Save lubiepomaranczki/ea6b6765708f782d5eba892476f337b3 to your computer and use it in GitHub Desktop.
BooksViewSource from SectionedCollectionView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BooksViewSource : MvxCollectionViewSource | |
{ | |
public IList<SectionItem> SectionedItemsCollection { get; set; } | |
public BooksViewSource(UICollectionView collectionView) | |
: base(collectionView, BookCell.Key) | |
{ | |
CollectionView.RegisterClassForCell(typeof(BookCell), BookCell.Key); | |
CollectionView.RegisterClassForSupplementaryView(typeof(BookHeader), UICollectionElementKindSection.Header, BookHeader.Key); | |
ReloadOnAllItemsSourceSets = true; | |
} | |
public override nint NumberOfSections(UICollectionView collectionView) | |
{ | |
if (SectionedItemsCollection.IsNullOrEmpty()) | |
{ | |
return 1; | |
} | |
return SectionedItemsCollection.Count; | |
} | |
public override nint GetItemsCount(UICollectionView collectionView, nint section) | |
{ | |
if (SectionedItemsCollection.IsNullOrEmpty() || SectionedItemsCollection.Count - 1 < (int)section) | |
{ | |
throw new Exception(nameof(GetItemsCount)); | |
} | |
var collection = SectionedItemsCollection[(int)section].Collection; | |
if (collection.IsNullOrEmpty()) | |
{ | |
throw new Exception("Collection can't be null or empty!"); | |
} | |
return collection.Count; | |
} | |
protected override object GetItemAt(NSIndexPath indexPath) | |
{ | |
return SectionedItemsCollection[indexPath.Section].Collection[indexPath.Row]; | |
} | |
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath) | |
{ | |
var headerView = (BookHeader)collectionView.DequeueReusableSupplementaryView(elementKind, BookHeader.Key, indexPath); | |
//TODO do whatever header initialization you need. | |
headerView.HeaderText = SectionedItemsCollection[indexPath.Section].HeaderText; | |
return headerView; | |
} | |
protected override UICollectionViewCell GetOrCreateCellFor(UICollectionView collectionView, | |
NSIndexPath indexPath, object item) | |
{ | |
return (UICollectionViewCell)CollectionView.DequeueReusableCell(BookCell.Key, indexPath); | |
} | |
public override void ReloadData() | |
{ | |
CollectionView.Layer.RemoveAllAnimations(); | |
base.ReloadData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment