-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS] Fix: CollectionView does not clear selection when SelectedItem is set to null #30420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,37 +56,49 @@ internal void SelectItem(object selectedItem) | |
| if (index.Section > -1 && index.Item > -1) | ||
| { | ||
| // Ensure the selected index is updated after the collection view's items generation is completed | ||
| CollectionView.PerformBatchUpdates(null, _ => | ||
| if (!CollectionView.IsLoaded()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this prevent item selection, if SelectedItem is pre-defined? please verify
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will not prevent item pre-selection as long as |
||
| { | ||
| // Ensure ItemsSource hasn't been disposed | ||
| if (ItemsSource is Items.EmptySource) | ||
| CollectionView.PerformBatchUpdates(null, _ => | ||
| { | ||
| return; | ||
| } | ||
| ValidateAndSelectItem(selectedItem, originalSource); | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| ValidateAndSelectItem(selectedItem, originalSource); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Exit if the ItemsSource reference no longer matches the one captured at invocation. | ||
| if (!ReferenceEquals(ItemsView.ItemsSource, originalSource)) | ||
| { | ||
| return; | ||
| } | ||
| private void ValidateAndSelectItem(object selectedItem, object originalSource) | ||
| { | ||
| // Ensure ItemsSource hasn't been disposed | ||
| if (ItemsSource is Items.EmptySource) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..) | ||
| var updatedIndex = GetIndexForItem(selectedItem); | ||
| if (updatedIndex.Section < 0 || updatedIndex.Item < 0) | ||
| { | ||
| return; | ||
| } | ||
| // Exit if the ItemsSource reference no longer matches the one captured at invocation. | ||
| if (!ReferenceEquals(ItemsView.ItemsSource, originalSource)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Retrieve the current item at that index and verify it still equals the intended selection. | ||
| var liveItem = GetItemAtIndex(updatedIndex); | ||
| if (!Equals(liveItem, selectedItem)) | ||
| { | ||
| return; | ||
| } | ||
| // Recalculate the index for the selectedItem now that the collection may have changed.(Adding, deleting etc..) | ||
| var updatedIndex = GetIndexForItem(selectedItem); | ||
| if (updatedIndex.Section < 0 || updatedIndex.Item < 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| CollectionView.SelectItem(index, true, UICollectionViewScrollPosition.None); | ||
| }); | ||
| // Retrieve the current item at that index and verify it still equals the intended selection. | ||
| var liveItem = GetItemAtIndex(updatedIndex); | ||
| if (!Equals(liveItem, selectedItem)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| CollectionView.SelectItem(updatedIndex, true, UICollectionViewScrollPosition.None); | ||
| } | ||
|
|
||
| // Called by Forms to clear the native selection | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 30363, "[iOS] CollectionView does not clear selection when SelectedItem is set to null", PlatformAffected.iOS)] | ||
| public class Issue30363 : ContentPage | ||
| { | ||
| public Issue30363() | ||
| { | ||
| ObservableCollection<string> Items = new ObservableCollection<string> | ||
| { | ||
| "Item 1", | ||
| "Item 2", | ||
| "Item 3", | ||
| "Item 4", | ||
| "Item 5" | ||
| }; | ||
|
|
||
|
|
||
| var collectionView = new CollectionView | ||
| { | ||
| ItemsSource = Items, | ||
| SelectionMode = SelectionMode.Single, | ||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| var label = new Label | ||
| { | ||
| FontSize = 24, | ||
| HorizontalOptions = LayoutOptions.FillAndExpand, | ||
| VerticalOptions = LayoutOptions.Center, | ||
| AutomationId = "cvItem" | ||
| }; | ||
| label.SetBinding(Label.TextProperty, "."); | ||
|
|
||
| return new Grid | ||
| { | ||
| Children = { label } | ||
| }; | ||
| }) | ||
| }; | ||
|
|
||
| collectionView.SelectionChanged += (sender, e) => | ||
| { | ||
| if (e.CurrentSelection.Count > 0) | ||
| { | ||
| var selectedItem = e.CurrentSelection[0] as string; | ||
| if (selectedItem != null) | ||
| { | ||
| // Deselect the item | ||
| collectionView.SelectedItem = null; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var grid = new Grid(); | ||
| grid.Children.Add(collectionView); | ||
|
|
||
| Content = grid; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue30363 : _IssuesUITest | ||
| { | ||
| public override string Issue => "[iOS] CollectionView does not clear selection when SelectedItem is set to null"; | ||
|
|
||
| public Issue30363(TestDevice device) | ||
| : base(device) | ||
| { } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void CollectionViewSelectionShouldClear() | ||
| { | ||
| App.WaitForElement("cvItem"); | ||
| App.Tap("cvItem"); | ||
| VerifyScreenshot(retryTimeout: TimeSpan.FromSeconds(2)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.