0

I have a MonoTouch.Dialog view controller with a custom element. My element implements IImageUpdated. In the GetCell method, I'm loading an image...

    var logo = ImageLoader.DefaultRequestImage (new Uri (url), this);
    if (logo == null) {
        cell.ImageView.Image = _defaultImage;
    } else {
        cell.ImageView.Image = logo;
    }

    #region IImageUpdated implementation

    public void UpdatedImage (Uri uri)
    {
       // HOW DO I GET THE CELL HERE?
       //cell.ImageView.Image = ImageLoader.DefaultRequestImage(uri, this);
    }

#endregion

In the UpdatedImage callback, how do I get access to the correct cell to update the image?

Rob Gibbens
  • 1,122
  • 1
  • 8
  • 24

2 Answers2

0

I recommend you subclass UITableViewCell, like ImageCell : UITableViewCell. Then you make a class that inherits from UIView (ImageCellView : UIView, IImageUpdate) and implements IImageUpdated. In that GetCell() you return an instance of your cell and and pass an ID or whatever to your cell. The ImageCellView instance is added to the cell's ContentView. Then each view should nicely know what to load and what to update and everything is encapsulated.

You can see an example in Miguel's TweetStation code: https://github.com/migueldeicaza/TweetStation/blob/master/TweetStation/UI/TweetCell.cs#L216

Krumelur
  • 32,180
  • 27
  • 124
  • 263
0

I like Krumelur's answer.

That said, this is a good observation: elements might not have an active cell at any point in time. In those cases, that means that the element is not yet made visible on the screen.

One thing that you can do, when the cell is requested, is to save the cell as a field, and later access it. The trick though is that by the time the image comes (say 30 seconds from now), your element might be out of the screen, and your cell would be invalid, so you would be updating someone else's cell. So you would nee to keep track of that cell as being still linked to your element and manually deal with that.

miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76