c# - 单击未调用的 UICollectionViewCell 中的特定元素(而是从 Source 触发的 ItemSelected)Xamarin.ios
问题描述
我的 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
当您点击按钮但不收回手指时将调用事件。
推荐阅读
- sql - 带有case语句的Excel sql db查询
- python - C#不能调用python和传递参数
- java - 给定算法的时间复杂度是多少?
- javascript - 如何使用 HTMLService / Google Script / Sheet 中的日期选择器返回值?
- mongodb - 当配置中未指定时,mongo 客户端如何知道要使用哪个 authMechanism?
- javascript - 如何使用样式化组件将按钮固定在标题右侧?
- c++ - 链接错误 cmake - 在 cmake 中使用 CodeSynthesis XSD
- node.js - output.globals 中没有为外部模块“ngx-cookie-service”提供名称——猜测是“ngxCookieService”
- microsoft-graph-api - 为多个用户订阅 MS Graph API 中的存在
- python - 如何在最近的 Windows 计算机上的 C:\Program Files 中安装 Python?