From 3fb3bd8a9b80351d02c9eb18896f6e38a40af874 Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 28 Nov 2023 13:53:10 -0500 Subject: [PATCH] Remove proxy class, use weakref --- .../Apple/CvCell.ios.maccatalyst.cs | 21 +++++-------------- .../Apple/CvDataSource.ios.maccatalyst.cs | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs index 63fc3ce..0fdc3d8 100644 --- a/VirtualListView/Apple/CvCell.ios.maccatalyst.cs +++ b/VirtualListView/Apple/CvCell.ios.maccatalyst.cs @@ -22,7 +22,7 @@ public CvCell(CGRect frame) : base(frame) this.ContentView.AddGestureRecognizer(new UITapGestureRecognizer(() => InvokeTap())); } - public TapHandlerProxy TapHandler { get; set; } + public WeakReference> TapHandler { get; set; } WeakReference keyCommands; @@ -55,7 +55,10 @@ public void KeyCommandSelect() void InvokeTap() { if (PositionInfo.Kind == PositionKind.Item) - TapHandler?.Invoke(this); + { + if (TapHandler?.TryGetTarget(out var handler) ?? false) + handler?.Invoke(this); + } } public void UpdateSelected(bool selected) @@ -133,18 +136,4 @@ public void SwapView(IView newView) VirtualView.SetTarget(newView); } } - - public class TapHandlerProxy - { - public TapHandlerProxy(Action tapHandler) - { - TapHandler = tapHandler; - } - - public Action TapHandler { get; set; } - - public void Invoke(CvCell cell) - => TapHandler?.Invoke(cell); - - } } \ No newline at end of file diff --git a/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs b/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs index 24f807b..96119e4 100644 --- a/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs +++ b/VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs @@ -40,7 +40,7 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS }; var cell = collectionView.DequeueReusableCell(nativeReuseId, indexPath) as CvCell; - cell.TapHandler = new CvCell.TapHandlerProxy(TapCellHandler); + cell.TapHandler = new WeakReference>(TapCellHandler); cell.Handler = Handler; cell.IndexPath = new WeakReference(indexPath);