1

I've got a UITableViewCell that displays text and optional an image. When the image is displayed however, it becomes a bit buggy because in -(void)prepareForReuse: I'm setting the imageCell of the tableViewCell to nil, and when scrolling the image needs to be loaded in every time.

In my customTableViewCell.m, this is the code I use to prepare for reuse:

- (void)prepareForReuse {
  [super prepareForReuse];

  self.noteLabel.text = nil;
  self.textLabel.text = nil;
  self.imageCell.image = nil;
  self.personImage = nil;
}

By deleting the row self.imageCell.image = nil; some cells will duplicate the imageCell from other UITableViewCells, so I have to use the prepareForReuse method.

Is there any way to not set the imageCell to nil if it has an image when all the cells are loaded? I've tried

if(self.iamgeCell.image == nil){
   self.imageCell.image = nil;
} 

In which I tried saying: if the imageCell was null before the reuse, please set it to nil when preparing for reuse, but that didn't work out so well.

This how I currently load the image in the cellForRowAtIndex method:

    PFFile *imageFile = [payment objectForKey:@"img"];
    if(![imageFile isEqual:@""]){
      [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
          UIImage *image = [UIImage imageWithData:imageData];
          cell.imageCell.image =  [self imageWithImage:image scaledToWidth:cell.imageCell.frame.size.width ];
            }
        }];
      }
bdv
  • 1,154
  • 2
  • 19
  • 42

1 Answers1

2

Several things. The logic in this code you posted is backwards, and won't do anything:

if(self.iamgeCell.image == nil){
   self.imageCell.image = nil;

In English that says "If the image is nil, set it to nil. You want the reverse:

if(self.iamgeCell.image != nil){
   self.imageCell.image = nil;

In English that says "If the image is NOT nil, set it to nil.

But you always want it nil, so why check, just set it to nil in prepareForReuse.

 self.imageCell.image = nil;

I don't usually implement prepareForReuse. Instead, in my data source methods, I always fully configure a cell, setting all fields to an explicit value (an image, or nil if they don't need an image) Think of this as re-using a paper form. You have to erase all the fields that the previous user wrote in.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Yeah I know, so that's what I have in my prepareForReuse right now, only thing is that this loads the image every time it becomes visible, which causes a slight glitch in the scrolling – bdv Oct 18 '14 at 14:41
  • Are you talking about loading an image from disk or downloading it? You need to use async downloading for the network. In either case, if an image is always the same for every cell, leave THAT image in place. Only erase data that can change between cells. Or if it's a static image that is shown sometimes and not others, hide the image if it's not used and un-hide it if it IS shown. – Duncan C Oct 19 '14 at 11:42