random
random

Reputation: 8608

UITableView custom cell images disappear after scrolling.

I am in the process of creating my open grid view.

I created a custom cell that looks like so:

enter image description here

I handle populating it like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"Cell";

    TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if( cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableGalleryCustomCell*)currentObject;
                break;
            }
        }
    }

    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.firstAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.firstPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.firstImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.firstImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.secondAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.secondPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.secondImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.secondImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.thirdAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.thirdPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.thirdImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.thirdImage.tag = countingIndex;
        }
    }

    countingIndex++;
    if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) {
        cell.fourthAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address];
        cell.fourthPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit];
        if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) {
            [cell.fourthImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]];
            cell.fourthImage.tag = countingIndex;
        }
    }

    countingIndex++;


    return cell;
}

It is a bit messy but it works.....until I scroll. After the image is loaded then scrolled off the screen the image disappears.

I believe that it maybe due to the imageViews of the cells being lost?

I tried the exact same implementation but with only 1 imageView in each cell and all works great.

Any point in the right direction would be very much appreciated.

I appreciate any help and thank you for your time.

Upvotes: 4

Views: 7971

Answers (3)

tsuyoski
tsuyoski

Reputation: 644

I had the similar problem, but solved by putting the actual image loading part inside "willDisplayCell" not in "cellForRow".

- (void)tableView:(UITableView *)tableView willDisplayCell:(CalendarTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.networkAccessAllowed = YES; //NO; //ver3.9.4
    options.synchronous  = NO;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
    options.resizeMode   = PHImageRequestOptionsResizeModeNone;
    options.version      = PHImageRequestOptionsVersionCurrent;
    options.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary *dictionary) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // self.progressView.progress = progress;
            // self.progressView.hidden = (progress <= 0.0 || progress >= 1.0);
        });
    };
    cell.tag = indexPath.row; //ver3.8.3

    cell.imageRequestID = [self.imageManager requestImageForAsset:asset
                                                               targetSize:targetSize
                                                              contentMode:PHImageContentModeAspectFit
                                                                  options:options
                                                            resultHandler:^(UIImage *result, NSDictionary *info) {
                                                                if (cell != nil) {
                                                                    if (cell.tag == indexPath.row) {
                                                                        dispatch_async(dispatch_get_main_queue(), ^{
                                                                            cell.photoImageView.image = result;
                                                                            cell.timesUpdated++;
                                                                            NSLog(@"cell.photoImageView.image SUCCESS!!");
                                                                            NSLog(@"indexPath.row = %ld",(long)indexPath.row);
                                                                        });
                                                                    }
                                                                }
                                                            }];


}

Upvotes: 0

Oded Ben Dov
Oded Ben Dov

Reputation: 10397

Sharing my experience:

Had similar problems. I have many tables in the app, and in one of them data would randomly disappear, and eventually the entire table would go away.

The problem for me was that I was dequeueing cells, giving all of them the same "unique" id. So several tables were sharing the same id, and I believe they were conflicting and messing up the cells I was looking at.

Giving each table it's own unique identifier solved the problem for me. (dah!)

Upvotes: 3

David M. Syzdek
David M. Syzdek

Reputation: 15788

I believe the problem is because your cells are being dequeued and you are populating cells in tableView:cellForRowAtIndexPath: using row information other than the indexPath.row.

Maybe try something along the following lines:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray         * rssItems;
    id                rssItem;
    static NSString * MyIdentifier = @"Cell";

    TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if( cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableGalleryCustomCell*)currentObject;
                break;
            }
        }
    }

    rssItems =  [[self.appDelegate rssParser] rssItems];
    if (indexPath.row < [rssItems count]) {
        rssItem = [rssItems objectAtIndex:indexPath.row];
        cell.firstAddress.text = [rssItem Address];
        cell.firstPrice.text   = [rssItem Deposit];
        if ([[rssItem imageURLs] count] != 0 ) {
            [cell.firstImage setImageWithURL:[NSURL URLWithString:[[rssItem imageURLs] objectAtIndex:0.0]]];
            cell.firstImage.tag = indexPath.row;
        }
    }

    return cell;
}

Upvotes: 2

Related Questions