Index '1' is greater than the number of rows '0'. (XF 240-Pre1 iOS)

RaymondKellyRaymondKelly USMember ✭✭✭

Just updated to 240-pre1 and now I cannot add items to an observable collection bound to a ListView with Uneven Rows and Groups. Have only tried iOS so far.

ObservableCollection<Group> groupedItems = new ObservableCollection<Group>();
listView.ItemsSource = groupedItems;
                        Group group1 = new Group("Text", "Text2");

                        Group group2 = new Group("Text", "Text2");
                        groupedItems.Add(group2);   ***** Crash here *********

The stack trace is emense. Here is the first portion.
" at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateItems (System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Int32 section, System.Boolean resetWhenGrouped) [0x0006b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:509 \n at Xamarin.Forms.Platform.iOS.ListViewRenderer.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:300 \n at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in C:\\BuildAgent3\\work\\ca3766cfc22354a1\\Xamarin.Forms.Core\\TemplatedItemsList.cs:758 \n at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnCollectionChangedGrouped (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000d5] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TemplatedItemsList.cs:785 \n at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Boolean fixWindows) [0x00008] in C:\\BuildAgent3\\work\\ca3766cfc22354a1\\Xamarin.Forms.Core\\TemplatedItemsList.cs:962 \n at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TemplatedItemsList.cs:955 \n at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListProxy.cs:233 \n at Xamarin.Forms.ListProxy+<>c__DisplayClass33_0.b__0 () [0x00018] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListProxy.cs:206 \n at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000a0] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListProxy.cs:225 \n at Xamarin.Forms.ListProxy+WeakNotifyProxy.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00031] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListProxy.cs:394 \n at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000f] in /Library/Frameworks/Xamarin.iOS.framework/Versions/ \n at System.Collections.ObjectModel.ObservableCollection1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/ \n

Am I doing something wrong? Thanks.


Best Answer


  • RaymondKellyRaymondKelly USMember ✭✭✭

    This does indeed work. Odd that it works fine on Android. Also, what about adding and removing groups. Do you have to unset itemsource before making any group changes changes?

  • MabroukMabrouk USMember ✭✭✭
    edited August 2017

    Just in your Event (that Edits or deletes a group) your code will be like :

    //Collection.Items = ..... or do any changes in your items (add, delete ...)
    MyListView.ItemsSource = Collection.Items;

    That's it :smile:

    Best regards from Regensburg,

  • So how then does Binding work? We're getting this error. Our list view is bound to an ObservableCollection in the XAML (using proper MVVM pattern). This worked in 2.3.x, but does not work in 2.4. I think this is a bug.

  • sgirouxsgiroux CAMember

    Same. Since the update this morning I have this exception... Very annoying!

  • ToemselToemsel USMember ✭✭
    edited October 2017

    Same issue since the last update.

  • stephenfosterstephenfoster USMember ✭✭
    edited October 2017

    We're in the process of updating our solutions to VS2017 and now have this issue with all our observable collections.

    Resetting the ItemSource doesn't work if the ObservableCollection is already bound to the control - as would be normal with MVVM.

    We've added BeginRefresh/EndRefresh - no luck.

    Do we know if this issue is being worked?

    We're at latest stable:

    I don't believe the exception refers to the ObservableCollection<>, as I get the following when there are 3 entries in the ObservableCollection: System.ArgumentException: Index '2' is greater than the number of rows '1'. occurred

    I suspect this refers to a collection being used by the grouped list view bound to this collection. IMHO...

    *** UPDATE ***

    I was able to work around this issue by creating and adding to a local collection and letting property change notification refresh the view:

    public ObservableCollection Items =
    get { return _items; }
    _items = value;
    private ObservableCollection _items = new ObservableCollection();

    private viod Refresh() // as an example...
    var items = new ObservableCollection();
    items.Add ( .... )
    items.Add ( .... )
    items.Add ( .... )

    this.Items = items; // use property setter to ensure OnPropertyChanged event


  • EltonSantanaEltonSantana USMember ✭✭
    edited October 2017

    I'm also facing the issue with version as described by Stephen Foster!

    It is related to this commit:

  • FoxsterFoxster GBMember ✭✭

    Facing the exact same issue described here; I spend more time fixing stuff that worked before each update! Very poor.

  • TienDinhTienDinh AUMember

    i'm having exactly the same issue with the latest nuget version.

  • AlexNAlexN RUMember ✭✭

    The save issue with latest stable: :(

  • ClayMartinClayMartin USUniversity ✭✭

    If you are using an MVVM pattern, one possible workaround is to add a DataLoaded action property to your view model. Invoke the DataLoaded action if it is not null after you have loaded all the data to the ObservableCollection property of your view model. In your code-behind C# file, subscribe to the DataLoaded action. In this event handler, bind the ItemsSource property of the ListView to the ObservableCollection property of your view model.

  • rayitarayita ESMember ✭✭

    Same issue at latest stable:

    We get this only in Binded ItemSource grouped list so must be an issued with ObservableCollection of groups not individual items.

    Only happends in iOS and not always.

  • I am avoid this exception with create different state (SatusGroup, DateGroup, WthoutGroup) and use different collection for every state.

  • RLERLE USMember

    Same here with latest stable:, only iOS. Had no problems in 2.3.*.
    Any comments from the Xamarin Team ?

  • kamukamu JPMember ✭✭

    This problem seems to be fixed current master branch.

    But current stable release (2.4.0 sr4) is not fixed.
    If urgent , there is such a solution as following code.

    [assembly: ExportRenderer(typeof(ListView), typeof(FixListViewRenderer))]
    namespace Sample.iOS
        public class FixListViewRenderer:ListViewRenderer
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e) {
                if (e.NewElement != null) {
                    var mCollectionChanged = typeof(ListViewRenderer).GetMethod("OnCollectionChanged", BindingFlags.Instance | BindingFlags.NonPublic);
                    Action<ITemplatedItemsList<Cell>> removeEvent = (items) => {
                        Delegate eventMethod = mCollectionChanged.CreateDelegate(typeof(NotifyCollectionChangedEventHandler), this);
                        items.GetType().GetEvent("CollectionChanged").RemoveEventHandler(items, eventMethod);
                    var templatedItems = ((ITemplatedItemsView<Cell>)e.NewElement).TemplatedItems;
                    templatedItems.CollectionChanged += TemplatedItems_CollectionChanged;
            protected override void Dispose(bool disposing) {
                    var templatedItems = ((ITemplatedItemsView<Cell>)Element).TemplatedItems;
                    templatedItems.CollectionChanged -= TemplatedItems_CollectionChanged;
            void TemplatedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  • kamukamu JPMember ✭✭

    This problem seems to have been fixed in 2.4.0 sr5.

  • AclrtrAclrtr USMember ✭✭
    edited November 2017

    This is still an issue for me in 2.4.0 sr5. Not only does it still crash when adding groups to the ListView, the ListView itself don't look right on iOS 11 in the latest service release, with the subheading being slightly indented, and the list much more indented. On all other versions of Xamarnin.Forms 2.4 there is no indentation.

    The error I get is:

        System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
        Parameter name: index
        at Xamarin.Forms.ListProxy.get_Item (System.Int32 index) [0x0000b] in D:\agent_work\1\s\Xamarin.Forms.Core\ListProxy.cs:127 

    This happens when I insert elements into a list that is the item source of a listview.

    Edit: I don't get an error when setting hasunewenrows to false, however then height of the cells are wrong but I guess I must set hasunewenrows to false and then fix the height?

  • AclrtrAclrtr USMember ✭✭
    edited November 2017

    Managed to recreate the padding bug in another test project. When there's a contentview in a viewcell there's extra space added on iOS 11, only in sr5.

  • RaymondKellyRaymondKelly USMember ✭✭✭

    I am still getting this using latest XF 2.5. SO frustrating. Anyone else found a workaround for this?

  • RaymondKellyRaymondKelly USMember ✭✭✭

    @aclrtr I confirmed your finding, setting uneven rows to false "fixes" the issue, but I need to have uneven rows.

  • kenneth.leekenneth.lee USMember ✭✭✭

    This is indeed frustrating ...

    This issue happened to me when i use ObservableCollection that contain groups and displaying on a ListView with databinding. The issue is not "fixed" in my case for toggling HasUnevenRows

  • ZoliZoli NLMember ✭✭
    edited February 9

    I also have the same issue on iOS, while it's fine on Android. Found this thread:
    And a 2(!) days old comment: "I can confirm, that it's finally fixed in"

    It works, no more crash.
    (however, updating my Xamarin.Forms to causing "System.IO.FileNotFoundException: Could not load assembly 'Xamarin.Android.Support.Fragment" for the Android project)

  • SureshKotambeSureshKotambe INMember

    Thank you
    Index '1' is greater than the number of rows '0'
    fixed This problem

Sign In or Register to comment.