SYNT4X
SYNT4X

Reputation: 210

Unrecognized selector sent to instance in UITableViewCell AccessoryView

I'm trying to add a plus button to my UITableViewCell.

I subclass a StringElement and add an UIImageView as my Cell AccessoryView.

To make it touchable, I add a UITapGestureRecognizer but everytime I try this, I get the same exception.

My GestureRecognizer and the ImageView are declared at class level of the StringElement.
What am I doing wrong?

My Code:

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();
    }
}

That's all. I grab the cell and add the Image and Recognizer to it. In this case, nothing happens.

When I add the recognizer to my TableView, I will get the exception.

I added the whole class of my Element. I hope this helps.

Upvotes: 0

Views: 841

Answers (1)

Greg Munn
Greg Munn

Reputation: 526

The problem is with

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

the target of the gesture recognizer is the image, you have defined tapped in the PrioritizeAndAddElement class. Try something like this instead

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"));

Upvotes: 3

Related Questions