Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a .net maui project involving the need to dive into native code to fix a well-known issue with the collection views not resizing when, say, expanding a section in a custom accordion on ios to view a nested collection view (surprise surprise).

I am stuck on getting each section item in a UICollectionView to reload when data has been updated so that the cell size can correctly update when data changes. Here is the code that I have so far for the OnItemSelected command when selecting an item in the collection view MAUI control:

C#
        public void OnSectionTapped(ItemObject section)
        {
            section.Expanded = !section.Expanded;
#if IOS
            var baseView = MainCollectionView.Handler.PlatformView;

            if(baseView != null)
            {
                var view = baseView as UIView;

                if(view != null)
                {
                    var subViewItemIsCollecitonView = view.Subviews[0] is UICollectionView;

                    if(subViewItemIsCollecitonView)
                    {
                        var iosCollectionView = (view.Subviews[0] as UICollectionView);
// This is the section where I am stuck
                        var items = iosCollectionView.GetIndexPathsForSelectedItems();

                        if(items != null)
                        {
                            var itemArr = items.ToArray();
                            var item = itemArr.FirstOrDefault();
                             
                            //iosCollectionView.ReloadItems(items.ToArray());
//This is the section where I am stuck
                        }
                    }

                }
            }
#endif
        }


What I have tried:

I've looked at the iOS docs, but I guess I'm having a hard time wrapping my head around getting the section and item indexes for the IndexPath to reload each item. I do apologize for the stupid question since this is my first time having to do this.

I also found a bug report on the maui project's github about this issue that contains a possibly good solution for this issue as shown below.

[BUG] Expander is not working in ios inside collectionview · Issue #1670 · CommunityToolkit/Maui · GitHub[^]

However, it involves creating a custom renderer and expander control for the list view, and I don't quite understand how the code for the OnAttachedListenerEffect is emplimented in the following line of code from the custom expander view:

C#
view.Effects.Add( new OnAttachedListenerEffect
        {
            OnAttachedToWindow = () =>
            {
                var closestExpanderViewCell = view.ClosestAncestor<ExpanderViewCell>();
                if ( closestExpanderViewCell is not null )
                {
                    if ( newValue is bool updateExpandedOnTapped )
                    {
                        if ( updateExpandedOnTapped )
                        {
                            view.GestureRecognizers.Add( new TapGestureRecognizer
                            {
                                Command = new Command( () => closestExpanderViewCell.IsExpanded = !closestExpanderViewCell.IsExpanded )
                            } );
                        }
                    }
                }
            }
        }
        );


The author says this about the on attached listener effect:
That property an OnAttachedListener effect that we've found useful to trigger an event when a view gets attached to the window.


So if no one knows how to get the first solution working, then it would be great to know how an effect like this can be written.

I know that these are technically two questions, and I do apologize for this, but I am just trying to cover as many solutions as possible since this issue with the collection views has been open for over a year now with no solid resolution for Microsoft. Any help would be much appreciated. Thanks.

[edit]
I should clarify that I did ask the autor to post the code on how this effect was written, but I got no response.
Posted
Updated 19-Jun-24 7:57am
v2

1 solution

If you follow the conversation, there is another similar issue mentioned: [iOS] CollectionView item cells do not resize when the content size is changed · Issue #21141 · dotnet/maui · GitHub[^]. Your best bet is to try the hacks suggested.

Next, I would use Google search to see if anyone has found a work-around. I've used the above GitHub issue title as a starting point: [iOS] CollectionView item cells do not resize when the content size is changed dotnet maui[^]
 
Share this answer
 
Comments
Baraiboapex 19-Jun-24 19:29pm    
Hi @Graeme_Grant I have looked over these, and unfortunately they will not work, because the lists we are using are quite large and have nested collection views as I mentioned earlier. I will look over these again, maybe I did something stupid, not sure how, but I will nonetheless try. ALso, as future reference, the Bindable layout is the solution I went with but this did not work in our case, because, again, our nested collection views are large. And this cause massive performance tanks during testing.
Baraiboapex 19-Jun-24 19:46pm    
I did find something of interest though while trying some of the other solutions again, It's this one where someone found a way to calculate the height of all the elements within an expander view: https://github.com/dotnet/maui/issues/21141#issuecomment-2039915759
Any idea on how this can be done? I've tried this before, but couldn't figure it out.
Baraiboapex 19-Jun-24 20:48pm    
Yeah, the other solutions didn't work for me. I'm going to try to figure out the height calculation one next. Any tips on how to do this would be nice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900