Skip to content
Draft
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
21 changes: 19 additions & 2 deletions src/Controls/src/Core/Handlers/Items/iOS/ItemsViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ protected virtual void UpdateTemplatedCell(TemplatedCell cell, NSIndexPath index

var bindingContext = ItemsSource[indexPath];

// If bindingContext is null (can happen during collection updates),
// we can't meaningfully update the cell, so return early to avoid issues
if (bindingContext is null)
{
return;
}

// If we've already created a cell for this index path (for measurement), re-use the content
if (_measurementCells != null && _measurementCells.TryGetValue(bindingContext, out TemplatedCell measurementCell))
{
Expand All @@ -467,7 +474,7 @@ protected virtual void UpdateTemplatedCell(TemplatedCell cell, NSIndexPath index
}
else
{
cell.Bind(ItemsView.ItemTemplate, ItemsSource[indexPath], ItemsView);
cell.Bind(ItemsView.ItemTemplate, bindingContext, ItemsView);
}

cell.LayoutAttributesChanged += CellLayoutAttributesChanged;
Expand Down Expand Up @@ -856,13 +863,23 @@ public UICollectionViewCell CreateMeasurementCell(NSIndexPath indexPath)
return cell;
}

// Check if bindingContext is null before creating measurement cell
var bindingContext = ItemsSource[indexPath];
if (bindingContext is null)
{
// Return a basic templated cell without binding if bindingContext is null
return CreateAppropriateCellForLayout();
}

TemplatedCell templatedCell = CreateAppropriateCellForLayout();

UpdateTemplatedCell(templatedCell, indexPath);

// Keep this cell around, we can transfer the contents to the actual cell when the UICollectionView creates it
if (_measurementCells != null)
_measurementCells[ItemsSource[indexPath]] = templatedCell;
{
_measurementCells[bindingContext] = templatedCell;
}

return templatedCell;
}
Expand Down