dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

Group Collection View - Expected dequeued view to be returned to the collection view in preparation for display #25878

Open Agistaris opened 4 hours ago

Agistaris commented 4 hours ago

Description

I've been using a group collection view to display grouped views for a while, traditional collection views do not have this issue. Only grouped collection views. The moment I populate either the Item source directly for the collection view or set the binding context to reference the item source for the collection view in XAML it has this issue and there seems to be no work around. This issue arose when using the iOS 18.1 simulator and when deployed on devices with iOS 18.1 and above (iOS 17.5 and below does not have this issue) using xcode 16+. This is also with .net8.0 and .NET 9.0 ,here is the exception I was able to catch:

ObjCRuntime.ObjCException: 'Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:]. Dequeued view: <Microsoft_Maui_Controls_Handlers_Items_VerticalSupplementaryView: 0x104acec40; baseClass = UICollectionViewCell; frame = (0 0; 0 0); alpha = 0; layer = <CALayer: 0x6000005c1420>>; Collection view: <Microsoft_Maui_Controls_Handlers_Items_MauiCollectionView: 0x127af7e00; baseClass = UICollectionView; frame = (0 0; 402 674); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000cd4ea0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x60000053fb60>; contentOffset: {0, 0}; contentSize: {402, 315}; adjustedContentInset: {0, 0, 539, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x104aa1520>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x104a9f190>>

Native stack trace:
    0   CoreFoundation                      0x00000001804b757c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x000000018008eda8 objc_exception_throw + 72
    2   Foundation                          0x0000000180e7a200 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00000001851dce74 __43-[UICollectionView _updateVisibleCellsNow:]_block_invoke.445 + 136
    4   UIKitCore                           0x0000000185ad0d4c -[_UICollectionViewSubviewManager removeAllDequeuedViewsWithEnumerator:] + 188
    5   UIKitCore                           0x00000001851dc93c -[UICollectionView _updateVisibleCellsNow:] + 4000
    6   UIKitCore                           0x00000001851e1744 -[UICollectionView layoutSubviews] + 284
    7   UIKitCore                           0x0000000186044bd8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2404
    8   QuartzCore                          0x000000018b0bcfe4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 432
    9   QuartzCore                          0x000000018b0c7d68 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
    10  QuartzCore                          0x000000018affca50 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 464
    11  QuartzCore                          0x000000018b02b260 _ZN2CA11Transaction6commitEv + 652
    12  QuartzCore                          0x000000018b02c7b4 _ZN2CA11Transaction25flush_as_runloop_observerEb + 68
    13  UIKitCore                           0x0000000185ad6c1c _UIApplicationFlushCATransaction + 48
    14  UIKitCore                           0x0000000185a07ccc __setupUpdateSequence_block_invoke_2 + 352
    15  UIKitCore                           0x000000018505d28c _UIUpdateSequenceRun + 76
    16  UIKitCore                           0x0000000185a07670 schedulerStepScheduledMainSection + 168
    17  UIKitCore                           0x0000000185a06aa8 runloopSourceCallback + 80
    18  CoreFoundation                      0x000000018041b7c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    19  CoreFoundation                      0x000000018041b70c __CFRunLoopDoSource0 + 172
    20  CoreFoundation                      0x000000018041aecc __CFRunLoopDoSources0 + 324
    21  CoreFoundation                      0x00000001804153b4 __CFRunLoopRun + 788
    22  CoreFoundation                      0x0000000180414c24 CFRunLoopRunSpecific + 552
    23  GraphicsServices                    0x000000019020ab10 GSEventRunModal + 160
    24  UIKitCore                           0x0000000185ad82fc -[UIApplication _run] + 796
    25  UIKitCore                           0x0000000185adc4f4 UIApplicationMain + 124
    26  libxamarin-dotnet-debug.dylib       0x00000001036af1e4 xamarin_UIApplicationMain + 60
    27  libmonosgen-2.0.dylib               0x0000000103f4da0c do_icall + 316
    28  libmonosgen-2.0.dylib               0x0000000103f4c208 do_icall_wrapper + 356
    29  libmonosgen-2.0.dylib               0x0000000103f4045c mono_interp_exec_method + 2632
    30  libmonosgen-2.0.dylib               0x0000000103f3df80 interp_runtime_invoke + 244
    31  libmonosgen-2.0.dylib               0x0000000103e511fc mono_jit_runtime_invoke + 1064
    32  libmonosgen-2.0.dylib               0x00000001040206ec mono_runtime_invoke_checked + 148
    33  libmonosgen-2.0.dylib               0x0000000104027e88 mono_runtime_exec_main_checked + 116
    34  libmonosgen-2.0.dylib               0x0000000103ea6bf0 mono_jit_exec + 364
    35  libxamarin-dotnet-debug.dylib       0x00000001036ef984 xamarin_main + 2324
    36  BiteBalance                         0x000000010234a48c main + 64
    37  dyld                                0x0000000103125410 start_sim + 20
    38  ???                                 0x0000000103252274 0x0 + 4347732596

Here is an example collection view:

<CollectionView 
    Grid.Row="1"
    Margin="{OnPlatform iOS='0,-20,0,0', Android='0,-45,0,0'}"
    x:Name="MainCollectionView"
    Scrolled="OnCollectionViewScrolled"
    IsGrouped="True"
    VerticalScrollBarVisibility="Never"
    HorizontalScrollBarVisibility="Never"
    ItemsSource="{Binding GroupedItems}"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">

    <CollectionView.GroupHeaderTemplate>
        <DataTemplate x:DataType="models:GroupModel">
            <Border 
                StrokeThickness="0" 
                HeightRequest="35" 
                Stroke="{Binding GroupColor}"
                StrokeShape="{RoundRectangle CornerRadius='20,20,0,0'}" 
                BackgroundColor="{DynamicResource HeaderBackgroundColor}">
                <Label x:Name="HeaderLabel" Margin="0" Text="{Binding HeaderText}" VerticalOptions="Center" HorizontalOptions="Center" FontSize="16" TextColor="White" />
            </Border>
        </DataTemplate>
    </CollectionView.GroupHeaderTemplate>

    <CollectionView.ItemsLayout>
        <LinearItemsLayout 
            Orientation="Vertical"
            SnapPointsType="None"
            SnapPointsAlignment="Start" />
    </CollectionView.ItemsLayout>

    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="models:ItemModel">
            <controls:ItemView 
                Padding="0,0"
                IsTapEnabled="False"
                SwipeLeftEnabled="True"
                SwipeRightEnabled="True"/>
        </DataTemplate>
    </CollectionView.ItemTemplate>

</CollectionView>

Steps to Reproduce

1. Set up the .NET MAUI Project:

2. Create Grouped Data Models:

3. Set up the CollectionView:

4. Bind Grouped Collection to CollectionView:

5. Populate the CollectionView via Binding:

6. Run on iOS 18.1 or Above:

Link to public reproduction project repository

No response

Version with bug

9.0.10 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

9.0.10 SR1

Affected platforms

iOS

Affected platform versions

iOS 18.1 and up

Did you find any workaround?

No work arounds. This does work with .NET 9.0 and .NET 8.0 if targeting a platform running on 17.5 and below. However, future releases will not work. that is why I said yes to regression testing but left the version as the current .NET release. because it does not work on newer iOS releases.

Relevant log output

similar-issues-ai[bot] commented 4 hours ago

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.