【问题标题】:Xamarin iOS (9.2): Gesture not working in emulatorXamarin iOS (9.2):手势在模拟器中不起作用
【发布时间】:2025-12-05 21:15:02
【问题描述】:

有人可以解释我的代码无法正常工作的原因吗?我正在尝试通过在UIPickerModel 中将UITableViewCell 添加到View 来在iOS 中创建多选UIPicker,然后将UITapGestureRecognizer 添加到每个单元格。

但模拟器不会响应我的触摸板的任何点击。

这是代码:

class PickerDataModel : UIPickerViewModel
{
    /*
    <summary>
        the items we wish to display
    </summary>
    */
    public List<string> Items { get; private set; }

    public PickerDataModel()
    {
        Items = new List<string>();
    }

    /*
    <summary>
        called by the picker to get the number of spinner items
    </summary>
    */    
    public override nint GetRowsInComponent(UIPickerView picker, nint component)
    {
        return Items.Count;
    }

    /* <summary>called by the picker to get the number of spinner items</summary> */    
    public override nint GetComponentCount(UIPickerView picker)
    {
        return 1;
    }

    /* <summary>called when a row is selected in the spinner</summary> */
    public override void Selected(UIPickerView picker, nint row, nint component)
    {

    }

    /* 
    <summary>
        Custom row view.
        The view param is the reusable view for the row. It will be null initially.

        You can add subviews or do anything within the view. But a lazy-initialization 
        block is preferred rather than every time this method is called.

        **Note** GetTitle() is no longer overridden since we aren't using 
        the default row view.
    </summary>
    */
    public override UIView GetView(UIPickerView picker, 
                                   nint row, nint component, UIView view)
    {
        if (view == null)
        {
            CGSize rowSize = picker.RowSizeForComponent(component);
            UITableViewCell cell = 
                    new UITableViewCell(new CGRect(new CGPoint(0, 0), rowSize));
            view = cell;
            cell.BackgroundColor = UIColor.Clear;
            cell.UserInteractionEnabled = true;
            UITapGestureRecognizer singleTapGestureRecognizer = 
                                            new UITapGestureRecognizer();
            singleTapGestureRecognizer.AddTarget(() => Console.WriteLine("Tapped"));
            singleTapGestureRecognizer.NumberOfTapsRequired = 1;
            cell.AddGestureRecognizer(singleTapGestureRecognizer);

            cell.TextLabel.Text = Items[(int) row];

            cell.Tag = row;                    
        }
        return view;
    }

    void Test()
    {
        Console.WriteLine("Tapped");
    }

    private void ToggleSelection(UITapGestureRecognizer recognizer)
    {                    
        ((UITableViewCell)recognizer.View).Accessory = UITableViewCellAccessory.Checkmark;
    }
}

【问题讨论】:

    标签: ios xamarin xamarin.ios uitapgesturerecognizer


    【解决方案1】:

    【讨论】: