Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 1 addition & 40 deletions src/Controls/src/Core/Handlers/Items2/iOS/TemplatedCell2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public event EventHandler<LayoutAttributesChangedEventArgs2> LayoutAttributesCha
Size _cachedConstraints;
Size _lastArrangedSize;
Size _lastNativeBoundsSize;
WeakReference<object> _itemsSource;

// Indicates the cell is being used as a supplementary view (group header/footer)
internal bool isSupplementaryView = false;
Expand Down Expand Up @@ -141,22 +140,7 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
if (cachedSize != CGSize.Empty)
{
_measuredSize = cachedSize.ToSize();
// For MeasureFirstItem, non-first cells reuse the cached first-item size.
// Mirroring CV1 (Items/TemplatedCell.cs): when ConstrainedSize is set, CV1
// calls NO virtualView.Measure() from PreferredLayoutAttributesFittingAttributes.
// We do the same — only call Measure when the layout constraints actually change
// (fresh cell: _cachedConstraints==default, or rotation/resize: new width/height).
// Checking the full Size (both width and height) handles both orientations:
// - Vertical list: constraints=(w, ∞) — width change drives the check.
// - Horizontal list: constraints=(∞, h) — height change drives the check.
// Recycled cells at the same constraints skip Measure entirely, eliminating
// the per-scroll MeasureOverride invocation that PR #29496 introduced.
// The Measure return value is intentionally discarded; _measuredSize comes
// from the cache, not from this call.
if (_cachedConstraints != constraints)
{
virtualView.Measure(constraints.Width, constraints.Height);
}
virtualView.Measure(constraints.Width, constraints.Height);
}
else
{
Expand Down Expand Up @@ -311,7 +295,6 @@ public void Bind(View virtualView, ItemsView itemsView)
void BindVirtualView(View virtualView, object bindingContext, ItemsView itemsView, bool needsContainer)
{
ResetArrangeState();
ResetCachedConstraintsIfItemsSourceChanged(itemsView.ItemsSource);

var oldElement = PlatformHandler?.VirtualView as View;

Expand Down Expand Up @@ -446,28 +429,6 @@ bool IsUsingVSMForSelectionColor(View view)

return false;
}
void ResetCachedConstraintsIfItemsSourceChanged(object itemsSource)
{
if (itemsSource is null)
{
if (_itemsSource is not null)
{
_itemsSource = null;
_cachedConstraints = default;
}

return;
}

if (_itemsSource is null ||
!_itemsSource.TryGetTarget(out var previousItemsSource) ||
!ReferenceEquals(previousItemsSource, itemsSource))
{
_itemsSource = new(itemsSource);
_cachedConstraints = default;
}
}

public override bool Selected
{
get => base.Selected;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
173 changes: 173 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue39534.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 39534, "CollectionView dates truncate after existing item properties change with MeasureFirstItem", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue39534 : ContentPage
{
private readonly Issue39534MainPageViewModel _viewModel;
private bool _toggle;

public Issue39534()
{
_viewModel = new Issue39534MainPageViewModel
{
Items = BuildItems()
};

BindingContext = _viewModel;

var grid = new Grid
{
RowDefinitions =
{
new RowDefinition(GridLength.Auto),
new RowDefinition(GridLength.Star)
}
};

var swapButton = new Button
{
Text = "Swap item properties",
Margin = 10,
AutomationId = "Issue39534SwapButton"
};

swapButton.Clicked += OnSwapClicked;
grid.Add(swapButton, 0, 0);

var collectionView = new CollectionView
{
AutomationId = "IssueCollectionView",
ItemSizingStrategy = ItemSizingStrategy.MeasureFirstItem,
ItemsSource = _viewModel.Items,
ItemTemplate = new DataTemplate(() =>
{
var itemGrid = new Grid
{
Padding = 10,
HeightRequest = 80,
ColumnDefinitions =
{
new ColumnDefinition(GridLength.Star),
new ColumnDefinition(new GridLength(115))
}
};

var titleLabel = new Label
{
TextColor = Color.FromArgb("#17191C")
};
titleLabel.SetBinding(
Label.TextProperty, nameof(IssueItem.Title));

itemGrid.Add(titleLabel, 0, 0);

var detailsLayout = new VerticalStackLayout
{
HorizontalOptions = LayoutOptions.End
};

detailsLayout.Children.Add(new Label
{
Text = "Last Issued",
FontSize = 10,
TextColor = Color.FromArgb("#4A4E55")
});

var dateLabel = new Label
{
AutomationId = "DateLabel",
HorizontalTextAlignment = TextAlignment.End,
TextColor = Color.FromArgb("#17191C")
};
dateLabel.SetBinding(
Label.TextProperty, nameof(IssueItem.DateText));

detailsLayout.Children.Add(dateLabel);
itemGrid.Add(detailsLayout, 1, 0);

return itemGrid;
})
};

grid.Add(collectionView, 0, 1);

Content = grid;
}

private void OnSwapClicked(object sender, EventArgs e)
{
_toggle = !_toggle;

var newItems = _toggle
? BuildItems().OrderBy(x =>
DateTime.Parse(x.DateText)).ToList()
: BuildItems().OrderByDescending(x =>
DateTime.Parse(x.DateText)).ToList();

for (int i = 0; i < newItems.Count; i++)
{
_viewModel.Items[i].Title = newItems[i].Title;
_viewModel.Items[i].DateText = newItems[i].DateText;
}
}

private static List<IssueItem> BuildItems() =>
[
new("Quarterly planning", "30-APR-2026"),
new("Design review", "15-MAY-2026"),
new("Release candidate", "28-MAY-2026"),
new("Documentation", "05-JUN-2026"),
new("Retrospective", "12-JUN-2026"),
new("Backlog review", "19-JUN-2026"),
new("Customer preview", "26-JUN-2026"),
new("General availability", "03-JUL-2026")
];
}

public sealed class Issue39534MainPageViewModel : BindableObject
{
public List<IssueItem> Items { get; set; } = [];
}

public class IssueItem : BindableObject
{
public IssueItem(string title, string dateText)
{
Title = title;
DateText = dateText;
}

private string _title = string.Empty;

public string Title
{
get => _title;
set
{
if (_title == value)
return;

_title = value;
OnPropertyChanged();
}
}

private string _dateText = string.Empty;

public string DateText
{
get => _dateText;
set
{
if (_dateText == value)
return;

_dateText = value;
OnPropertyChanged();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue39534 : _IssuesUITest
{
public Issue39534(TestDevice device) : base(device)
{
}

public override string Issue => "CollectionView dates truncate after existing item properties change with MeasureFirstItem";

[Test]
[ShardedTestCategory(UITestCategories.CollectionView, shard: 1)]
public void DateLabelsShouldFitFullTextAfterExistingItemPropertiesAreSwapped()
{
App.WaitForElement("Issue39534SwapButton");

for (int swapCount = 1; swapCount <= 6; swapCount++)
{
App.Tap("Issue39534SwapButton");
}

VerifyScreenshot();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading