Skip to content

Commit bafd036

Browse files
authored
Merge pull request #16 from Redth/scroll-to-item
Fix iOS/MacCat cell tap for selection
2 parents d5bf990 + 1c49355 commit bafd036

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

VirtualListView/Apple/CvCell.ios.maccatalyst.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public CvCell(CGRect frame) : base(frame)
2323
this.ContentView.AddGestureRecognizer(new UITapGestureRecognizer(() => InvokeTap()));
2424
}
2525

26-
public WeakReference<Action<CvCell>> TapHandler { get; set; }
26+
private TapHandlerCallback TapHandler;
27+
28+
public void SetTapHandlerCallback(Action<CvCell> callback)
29+
{
30+
TapHandler = new TapHandlerCallback(callback);
31+
}
2732

2833
WeakReference<UIKeyCommand[]> keyCommands;
2934

@@ -57,8 +62,7 @@ void InvokeTap()
5762
{
5863
if (PositionInfo.Kind == PositionKind.Item)
5964
{
60-
if (TapHandler?.TryGetTarget(out var handler) ?? false)
61-
handler?.Invoke(this);
65+
TapHandler.Invoke(this);
6266
}
6367
}
6468

@@ -142,4 +146,17 @@ public void UpdatePosition(PositionInfo positionInfo)
142146
viewPositionInfo.Update(positionInfo);
143147
}
144148
}
149+
150+
class TapHandlerCallback
151+
{
152+
public TapHandlerCallback(Action<CvCell> callback)
153+
{
154+
Callback = callback;
155+
}
156+
157+
public readonly Action<CvCell> Callback;
158+
159+
public void Invoke(CvCell cell)
160+
=> Callback?.Invoke(cell);
161+
}
145162
}

VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
3939
_ => "UNKNOWN",
4040
};
4141

42-
var cell = collectionView.DequeueReusableCell(nativeReuseId, indexPath) as CvCell;
43-
cell.TapHandler = new WeakReference<Action<CvCell>>(TapCellHandler);
42+
var cell = (collectionView.DequeueReusableCell(nativeReuseId, indexPath) as CvCell)!;
43+
cell.SetTapHandlerCallback(TapCellHandler);
4444
cell.Handler = Handler;
4545
cell.IndexPath = new WeakReference<NSIndexPath>(indexPath);
4646

0 commit comments

Comments
 (0)