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:
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);
var items = iosCollectionView.GetIndexPathsForSelectedItems();
if(items != null)
{
var itemArr = items.ToArray();
var item = itemArr.FirstOrDefault();
}
}
}
}
#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:
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.