gpt4 book ai didi

c# - 单击未调用的 UICollectionViewCell 中的特定元素(而不是从源中触发的 ItemSelected)Xamarin.ios

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:54 24 4
gpt4 key购买 nike

我的 Xamarin.iOS 项目中有一个 CollectionView。我的 UICollectionViewCell 中有一个按钮,我想在我的按钮上设置 TouchDown。

但是当我点击这个按钮时,触发了 ItemSelected 而不是我的 TouchDown 事件处理程序(调用单元格上的全局触摸而不是特定触摸)。

你能帮我如何在任何单元格内指定我的触摸吗? (对不起,我的英语不是最好的)

这是我的代码:

class MyCollectionViewSource : UICollectionViewSource
{
List<string> _Datas;

public MyCollectionViewSource(List<string> datas)
{
_Datas = datas;
}

public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return _Datas.Count;
}

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{

var view = (MyCollectionViewCell)collectionView.DequeueReusableCell(MyCollectionViewCell.CellId, indexPath);
return view;
}

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
Console.WriteLine("Row {0} selected", indexPath.Row);
}

public override bool ShouldSelectItem(UICollectionView collectionView, NSIndexPath indexPath)
{
return true;
}
}

class MyCollectionViewCell : UICollectionViewCell
{
UIView _MyCellView;

public static readonly NSString CellId = new NSString("MyCollectionViewCell");

[Export("initWithFrame:")]
MyCollectionViewCell(RectangleF frame) : base(frame)
{
_MyCellView = new UIView();
ContentView.AddSubview(_MyCellView);

UIButton btn = new UIButton();
btn.SetTitle("test", UIControlState.Normal);
btn.Frame = new CoreGraphics.CGRect(0, 0, 100, 20);
btn.TouchDown += Btn_TouchDown;

_MyCellView.AddSubview(btn);

}

private void Btn_TouchDown(object sender, EventArgs e)
{

}
}

最佳答案

如果要设置Button的点击事件。事件名称为

btn.TouchUpInside += Btn_TouchUpInside;

事件 TouchDown 将在您点击按钮但未收回手指时调用。

关于c# - 单击未调用的 UICollectionViewCell 中的特定元素(而不是从源中触发的 ItemSelected)Xamarin.ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57623781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com