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");
                        groupedItems.Add(group1);

                        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/10.12.0.18/src/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288 \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/10.12.0.18/src/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351 \n

Am I doing something wrong? Thanks.

Tagged:

Best Answer

Answers

  • 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 18

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

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

    That's it :smile:

    Best regards from Regensburg,
    Mabrouk.

  • 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 2

    Same issue since the last update.

  • stephenfosterstephenfoster USMember
    edited October 3

    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: 2.4.0.282.

    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; }
    set
    {
    _items = value;
    OnPropertyChanged("Items");
    }
    }
    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 4

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

    It is related to this commit: github.com/xamarin/Xamarin.Forms/commit/6f5d6ce8daf280ce3f5717cb90ee2e73ae9e474b

  • 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: 2.4.0.282 :(

  • 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: 2.4.0.282

    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: 2.4.0.282, 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.

    github.com/xamarin/Xamarin.Forms/blob/0a0f8f092eaf335677eaef8321f579c6e31c8e0a/Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs#L515-L521

    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) {
    
                base.OnElementChanged(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;
                    removeEvent(templatedItems);
                    templatedItems.CollectionChanged += TemplatedItems_CollectionChanged;
    
                }
            }
    
            protected override void Dispose(bool disposing) {
                if(disposing){
                    var templatedItems = ((ITemplatedItemsView<Cell>)Element).TemplatedItems;
                    templatedItems.CollectionChanged -= TemplatedItems_CollectionChanged;
                }
                base.Dispose(disposing);
            }
    
    
            void TemplatedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {            
                Control.ReloadData();          
            }
        }
    }
    
  • kamukamu JPMember ✭✭

    This problem seems to have been fixed in 2.4.0 sr5.

  • AclrtrAclrtr USMember ✭✭
    edited November 13

    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 14

    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.

Sign In or Register to comment.