1

I'm trying to customise the BubbleCell sample in Xamarin's MonoTouch samples set. I want to add a UIImageView into the chat bubbles, and make it so when the UIImageView is tapped, the code can do something (in my case, open up a modal view - although for now I'll settle for writing something to the output window).

The sample uses MonoTouch.Dialog to lay out the interface, and has a custom cell class that constructs the view and adds it to the UITableViewCell.ContentView.

I've managed to get the UIImageView into the cell fairly easily. However, I can't seem to work out how to make the UIImageView respond to taps.

I'm doing the construction of the UIImageView in the constructor of BubbleCell (in Bubble.cs), and at the same time, I add the following lines in:

var tap = new UITapGestureRecognizer();
tap.AddTarget( () => {
    Console.WriteLine("I'll do something here");
});
imageView.UserInteractionEnabled = true;
imageView.AddGestureRecognizer(tap);

The code compiles and runs fine, but when I tap the UIImageView, nothing happens.

I've tried a few changes, none of which fixed this:

  • I swapped out the UIImageView for a custom UIButton.
  • I went through the superview hierarchy of both the cell and the UITableView and made sure all of the superviews have the UserInteractionEnabled properties set to true. They do.
  • I removed the UITapGestureRecognizer and tried subclassing UIImageView and intercepting the TouchesBegan/TouchesEnded methods (they were never called)

Is there anything special I need to do to make the table cells within MonoTouch.Dialog work with user interaction?

John
  • 5,452
  • 8
  • 37
  • 37
  • Does responding to touchesBegan not work? See [this question](http://stackoverflow.com/questions/855095/how-can-i-detect-the-touch-event-of-an-uiimageview) for a similar scenario – Jason Jun 26 '12 at 11:50
  • No, unfortunately - as I said in the question, subclassing UIImageView and responding to touchesBegan didn't work - none of the TouchesBegan/TouchesEnded etc methods were ever invoked. I also overrode the CanBecomeFirstResponder method in the subclass and returned true, and this didn't help either. – John Jun 26 '12 at 12:24
  • sorry, missed that bit in your question. I think you should be able to do it that way, without using gestures, but I'm not sure why that's not working. – Jason Jun 26 '12 at 12:49
  • UIImageView needs the property "UserInteractionEnabled" to be set to TRUE, It is FALSE by default. Then your touches will work. – Krumelur Jul 04 '12 at 20:30
  • As per the code above, I've set imageView.UserInteractionEnabled = true in the code I'm using :) – John Jul 05 '12 at 02:25
  • Thanks to everyone who's tried answering this. I tried again with this and just couldn't figure out the way to do it within the way the sample code worked. In the end, I changed approach and just handle the UITableView's RowSelected delegate method - this is a bit more heavy-handed than I'd like (i.e. it fires no matter where on the row I tap), but at least it works. If I get time, I'll revisit this to try to make it behave the way I wanted originally, and will repost here if I do. :) – John Jul 09 '12 at 19:38

1 Answers1

1

How do you create the UIImageView object? I've just made a sample project and it react to taps perfectly.

UIImageView btn = new UIImageView (new UIImage ("50-50.png"));
btn.UserInteractionEnabled = true;
UITapGestureRecognizer rec = new UITapGestureRecognizer ();
rec.AddTarget (() => Console.WriteLine ("Test"));
btn.AddGestureRecognizer (rec);
Root = new RootElement ("TestViewController") {
    new Section ("First Section"){
        new UIViewElement ("Test", btn, false)
    },
    new Section ("Second Section"){
    },
};
Ivan Nikitin
  • 3,578
  • 27
  • 39