0

我正在尝试向我的UITableViewCell.

我将一个 StringElement 子类化并添加一个UIImageView作为我的 Cell AccessoryView。

为了使它可触摸,我添加了一个,UITapGestureRecognizer但每次我尝试这个时,我都会遇到同样的异常。

我的 GestureRecognizer 和 ImageView 在 StringElement 的类级别声明。
我究竟做错了什么?

我的代码:

public class PrioritizeAndAddElement : StringElement
{
    NSAction Plus;
    UITapGestureRecognizer tap;
    UIImageView image;

    public PrioritizeAndAddElement (string caption, NSAction select, NSAction plus) : base(caption, select)
    {
        Plus = plus;
    }

    public override UITableViewCell GetCell (UITableView tv)
    {   
        UITableViewCell cell = base.GetCell(tv);
        image = new UIImageView(new UIImage("images/app/greenbutton.png"));
        image.UserInteractionEnabled = true;
        image.Frame = new RectangleF(cell.Frame.Width - 65, 14, 25, 25);
        cell.AccessoryView = image;
        tap = new UITapGestureRecognizer(image, new Selector("tapped"));
        image.AddGestureRecognizer(tap);
        return cell;
    }

    [Export("tapped")]
    public void tapped(UIGestureRecognizer sender){
        if(Plus != null)
            Plus();
    }
}

就这样。我抓住单元格并将图像和识别器添加到它。在这种情况下,什么都不会发生。

当我将识别器添加到我的 TableView 时,我会得到异常。

我添加了我的元素的整个类。我希望这有帮助。

4

1 回答 1

3

问题在于

tap = new UITapGestureRecognizer(>>>image<<<, new Selector("tapped"));

手势识别器的目标是图像,您在 PrioritizeAndAddElement 类中定义了 tapped。尝试这样的事情

public class TappableImageView : UIImageView
{
    NSAction Plus;

    public TappableImageView(NSAction plus, UIImage img) : base(img)
    {
        this.Plus = plus;
    }

    [Export("tapped:")]
    public void Tapped(UIGestureRecognizer sender)
    {
        if(Plus != null)
            Plus();
    }
}

...

image = new TappableImageView(new UIImage("images/brushTexture1.png"));
于 2013-02-15T10:48:22.197 回答