Skip to content

[iOS & Mac] Fix for Incorrect ItemsViewScrolledEventArgs Values in CollectionView with Grouped Items#34240

Open
SyedAbdulAzeemSF4852 wants to merge 1 commit intodotnet:mainfrom
SyedAbdulAzeemSF4852:fix-17664_ios
Open

[iOS & Mac] Fix for Incorrect ItemsViewScrolledEventArgs Values in CollectionView with Grouped Items#34240
SyedAbdulAzeemSF4852 wants to merge 1 commit intodotnet:mainfrom
SyedAbdulAzeemSF4852:fix-17664_ios

Conversation

@SyedAbdulAzeemSF4852
Copy link
Contributor

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 commented Feb 25, 2026

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details

  • When a CollectionView's IsGrouped property is set to true, the values of FirstVisibleItemIndex, CenterItemIndex, and LastVisibleItemIndex in the ItemsViewScrolledEventArgs passed to the Scrolled event handler are incorrect.

Root Cause

  • Visible items were sorted only by Row, which produced incorrect ordering when items from multiple sections were visible simultaneously.

Description of Change

iOS (ItemsViewDelegator.cs and ItemsViewDelegator2.cs)

  • Changed the sort from .OrderBy(x => x.Row) to .OrderBy(x => x.Section).ThenBy(x => x.Row) so that IndexPathsForVisibleItems is ordered correctly across section boundaries. The fix is applied to both the legacy handler (Items/) and the current handler (Items2/).

Issues Fixed

Fixes #17664

Validated the behaviour in the following platforms

  • Windows
  • Android
  • iOS
  • Mac

Android fix PR: #31437

Output

Before After
iOS_Before.mov
iOS_After.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Feb 25, 2026
@dotnet-policy-service
Copy link
Contributor

Hey there @@SyedAbdulAzeemSF4852! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Feb 25, 2026
@sheiksyedm sheiksyedm added area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/ios labels Feb 26, 2026
@sheiksyedm sheiksyedm marked this pull request as ready for review February 26, 2026 12:44
Copilot AI review requested due to automatic review settings February 26, 2026 12:44
@sheiksyedm
Copy link
Contributor

/azp run maui-pr-uitests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect ItemsViewScrolledEventArgs visible-item indices for grouped CollectionView on iOS/Mac by ensuring visible NSIndexPaths are ordered consistently across section boundaries, and adds a HostApp repro page + Appium UI test for issue #17664.

Changes:

  • iOS (legacy Items/) and iOS/MacCatalyst (current Items2/) delegators now sort visible NSIndexPaths by Section then Row.
  • Added HostApp issue page (Issue17664) that displays the “last visible item” text while scrolling grouped content.
  • Added an Appium UI test (Issue17664) validating the label updates after a programmatic scroll.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
src/Controls/src/Core/Handlers/Items2/iOS/ItemsViewDelegator2.cs Updates visible index-path ordering for grouped sources (Items2 iOS/MacCatalyst handler).
src/Controls/src/Core/Handlers/Items/iOS/ItemsViewDelegator.cs Updates visible index-path ordering for grouped sources (legacy iOS handler).
src/Controls/tests/TestCases.HostApp/Issues/Issue17664.cs Adds repro page using grouped CollectionView + Scrolled handler output.
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue17664.cs Adds Appium UI regression test for the issue.

Comment on lines +121 to 123
// Sort visible item index paths by section and then by row for consistent order in both grouped and ungrouped sources
var indexPathsForVisibleItems = collectionView.IndexPathsForVisibleItems.OrderBy(x => x.Section).ThenBy(x => x.Row).ToList();

Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetVisibleItemsIndexPath now orders IndexPathsForVisibleItems by Section/Row, but GetCenteredIndexPath still orders only by Row. If IndexPathForItemAtPoint returns null (e.g., center point lands between cells/headers), the fallback uses the incorrectly-sorted "first" index path, which can still produce wrong CenterItemIndex for grouped sources. Update GetCenteredIndexPath to use the same Section/Row ordering for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +120 to 122
// Sort visible item index paths by section and then by row for consistent order in both grouped and ungrouped sources
var indexPathsForVisibleItems = collectionView.IndexPathsForVisibleItems.OrderBy(x => x.Section).ThenBy(x => x.Row).ToList();

Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetVisibleItemsIndexPath now orders IndexPathsForVisibleItems by Section/Row, but GetCenteredIndexPath still orders only by Row. If IndexPathForItemAtPoint returns null (e.g., center point lands between cells/headers), the fallback uses the incorrectly-sorted "first" index path, which can still produce wrong CenterItemIndex for grouped sources. Update GetCenteredIndexPath to use the same Section/Row ordering for consistency.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,31 @@
#if TEST_FAILS_ON_ANDROID // PR Link - https://github.com/dotnet/maui/pull/31437
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test file is wrapped in #if TEST_FAILS_ON_ANDROID, which means it will never run in the Android UI test project (where TEST_FAILS_ON_ANDROID is not defined). If the intent is to validate the cross-platform fix (and especially if the linked Android fix has landed), remove this guard or switch to a runtime skip so Android gets regression coverage too.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +23 to +28
#if WINDOWS
Thread.Sleep(1000);
#endif

var resultItem = App.WaitForElement("Issue17664DescriptionLabel").GetText();
Assert.That(resultItem, Is.EqualTo("Category C item #2"));
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid platform-specific #if WINDOWS + Thread.Sleep in the test body; it’s brittle and can still be flaky. Prefer waiting for the expected UI state (e.g., wait until the label text becomes the expected value using an existing wait helper) so the test is stable across platforms without conditional compilation.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +57 to +64
_collectionView.Scrolled += (s, e) =>
{
var flatItems = _groupedItems.SelectMany(group => group).ToList();
if (e.LastVisibleItemIndex < flatItems.Count)
{
descriptionLabel.Text = flatItems[e.LastVisibleItemIndex];
}
};
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Scrolled handler assumes LastVisibleItemIndex is always non-negative. On some platforms/conditions ItemsViewScrolledEventArgs can report -1, and the current check (< flatItems.Count) would still pass and then index flatItems[-1], causing an exception. Add a >= 0 guard (or otherwise handle -1) before indexing into flatItems.

Copilot uses AI. Check for mistakes.

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 17664, "Incorrect ItemsViewScrolledEventArgs in CollectionView when IsGrouped is set to true", PlatformAffected.iOS | PlatformAffected.Android)]
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IssueAttribute marks this as affecting iOS/Android only, but this PR targets iOS + Mac as well. Consider adding PlatformAffected.macOS (and any other affected platforms) so the metadata matches where this scenario is expected to reproduce/be validated.

Suggested change
[Issue(IssueTracker.Github, 17664, "Incorrect ItemsViewScrolledEventArgs in CollectionView when IsGrouped is set to true", PlatformAffected.iOS | PlatformAffected.Android)]
[Issue(IssueTracker.Github, 17664, "Incorrect ItemsViewScrolledEventArgs in CollectionView when IsGrouped is set to true", PlatformAffected.iOS | PlatformAffected.Android | PlatformAffected.macOS)]

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS/Mac/Windows] CollectionView ItemsViewScrolledEventArgs are incorrect when IsGrouped = true

3 participants