0

I have a Tableview containing cells with images, however the cells that are reused still contain the image from the previous cell that is being reused until the new image is downloaded and set.

I have tried setting the image to nil. (imageV is a subclass of HJManagedImageV, source can be found here: HJManagedImageV

[cell.imageV setImage:nil];

the setter

-(void)setImage:(UIImage *)theImage{
    if (theImage==image) {
        //when the same image is on the screen multiple times, an image that is alredy set might be set again with the same image.
        return; 
    }
    [theImage retain];
    [image release];
    image = theImage;
    [imageView removeFromSuperview];
    self.imageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
    [self addSubview:imageView];
    [imageView setNeedsLayout];
    [self setNeedsLayout];
    [loadingWheel stopAnimating];
    [loadingWheel removeFromSuperview];
    self.loadingWheel = nil;
    self.hidden=NO;
    if (image!=nil) {
        [callbackOnSetImage managedImageSet:self];
    }
}

I have a workaround for by setting imageV to hidden but then I lose the loading spinner and I'd really like to know why setting it to nil isn't working.

Anyone have any ideas, cause I'm all out of them. Or am I missing a step somewhere.

triggs
  • 5,890
  • 3
  • 32
  • 31

2 Answers2

3

Are the cells a subclass of UITableViewCell? If yes, I would try to implement the method prepareForReuse and see if can solve the problem there.

Hope this helps =)

GreyHands
  • 1,734
  • 1
  • 18
  • 30
  • 2
    from the link you posted: "For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell." So I don't think that the way to go. – triggs May 17 '12 at 12:37
  • Never saw a performance problem using this method while setting a UIImageView to nil or to a default image. But if you do not want even to try this method then the answer from Bogdan Bucur is your best bet (IMHO). – GreyHands May 17 '12 at 13:37
  • I just tried setting the image in the imageview to nil in prepareForReuse but it still showed up. I put a default image, happened to use a semi transparent image and the previous image could still be seen beneath it. Do you know why this happens? Is there some sort of drawing cache thats still being used or something along those lines. I have put a blank white image and that works but I'd still like to know why setting it to nil isn't working. – triggs May 17 '12 at 15:25
  • Are you setting the image in the correct UIImageView? Setting a semi transparent image should never show the old image beneath it. – GreyHands May 17 '12 at 15:47
  • Almost positive its the right one, setting the same imageView to hidden causes it to disappear. – triggs May 17 '12 at 16:06
  • 1
    Silly question: did you tried the _clear_ method that comes with HJManagedImageV? – GreyHands May 17 '12 at 16:38
  • I did and it had no effect, its almost as if there's a second imageview somewhere, (this may be a stupid thought but I was only just dropped into this code base with no iOS experience so I'm a little out of my depth)is it possible that both a synthesized instance variable and an ivar are both somehow being created and having the image set – triggs May 17 '12 at 17:01
  • It's dinner time for me, if you want we can move to chat later. – GreyHands May 17 '12 at 17:32
  • Thanks for the offer but I think I've spent too much time on this issue already, setting a white image works so I guess I'll have to leave my curiosity unsatisfied, cheers for your help. – triggs May 18 '12 at 08:48
1

Don't set it to nil, nor hide it. We use a similar class and what we do for such cases is to set a start image to be displayed until the new image loads. So in your case you could simply set the image as a blank jpg/png inside your bundle instead of nil

BBog
  • 3,630
  • 5
  • 33
  • 64
  • I put a default image, happened to use a semi transparent image and the previous image could still be seen beneath it. Do you know why this happens? Is there some sort of drawing cache thats still being used or something along those lines. I have put a blank white image and that works but I'd still like to know why setting it to nil isn't working. – triggs May 17 '12 at 15:26
  • As far as I know, setting something to nil is actually a sort of release statement, it doesn't actually empty the object, not if there are other things that retain it. I'm not 100% sure on this, it's what a colleague of mine told me. – BBog May 21 '12 at 06:17
  • Ah, there you go, about the nil part: http://stackoverflow.com/questions/4739937/ios-memory-management-followup-dealloc-vs-nil – BBog May 21 '12 at 06:17