diff --git a/Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs b/Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs index d493321c51c..b96ee644155 100644 --- a/Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs +++ b/Xamarin.Forms.Platform.iOS/CollectionView/ObservableItemsSource.cs @@ -25,10 +25,12 @@ public ObservableItemsSource(IEnumerable itemSource, UICollectionViewController _itemsSource = itemSource as IList ?? itemSource as IEnumerable; + Count = ItemsCount(); + ((INotifyCollectionChanged)itemSource).CollectionChanged += CollectionChanged; } - public int Count => ItemsCount(); + public int Count { get; private set; } public object this[int index] => ElementAt(index); @@ -52,7 +54,7 @@ protected virtual void Dispose(bool disposing) public int ItemCountInGroup(nint group) { - return ItemsCount(); + return Count; } public object Group(NSIndexPath indexPath) @@ -62,7 +64,7 @@ public object Group(NSIndexPath indexPath) public NSIndexPath GetIndexForItem(object item) { - for (int n = 0; n < ItemsCount(); n++) + for (int n = 0; n < Count; n++) { if (this[n] == item) { @@ -75,7 +77,7 @@ public NSIndexPath GetIndexForItem(object item) public int GroupCount => 1; - public int ItemCount => ItemsCount(); + public int ItemCount => Count; public object this[NSIndexPath indexPath] { @@ -118,6 +120,7 @@ void Reload() { _collectionView.ReloadData(); _collectionView.CollectionViewLayout.InvalidateLayout(); + Count = ItemsCount(); } NSIndexPath[] CreateIndexesFrom(int startIndex, int count) @@ -141,40 +144,42 @@ bool NotLoadedYet() void Add(NotifyCollectionChangedEventArgs args) { - var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : IndexOf(args.NewItems[0]); - var count = args.NewItems.Count; - if (NotLoadedYet()) { _collectionView.ReloadData(); return; } + var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : IndexOf(args.NewItems[0]); + var count = args.NewItems.Count; + if (!_grouped && _collectionView.NumberOfItemsInSection(_section) == 0) { // Okay, we're going from completely empty to more than 0 items; there's an iOS bug which apparently // will just crash if we call InsertItems here, so we have to do ReloadData. _collectionView.ReloadData(); + Count += count; return; } _collectionView.PerformBatchUpdates(() => - { - var indexes = CreateIndexesFrom(startIndex, count); - _collectionView.InsertItems(indexes); - }, null); + { + var indexes = CreateIndexesFrom(startIndex, count); + _collectionView.InsertItems(indexes); + Count += count; + }, null); } void Remove(NotifyCollectionChangedEventArgs args) { - var startIndex = args.OldStartingIndex; - if (NotLoadedYet()) { _collectionView.ReloadData(); return; } + var startIndex = args.OldStartingIndex; + if (startIndex < 0) { // INCC implementation isn't giving us enough information to know where the removed items were in the @@ -189,6 +194,7 @@ void Remove(NotifyCollectionChangedEventArgs args) _collectionView.PerformBatchUpdates(() => { _collectionView.DeleteItems(CreateIndexesFrom(startIndex, count)); + Count -= count; }, null); }