首页 > 解决方案 > 单击未调用的 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)
    {

    }
}

标签: c#xamarin.iosuicollectionviewcelluicollectionviewflowlayout

解决方案


如果要设置的点击事件Button。活动名称是

btn.TouchUpInside += Btn_TouchUpInside;

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


推荐阅读