0

Hello I have a script that downloads an image and dynamically sets the UITableView height after it finishes the download, but some images randomly do not show , the curious think is that when i change back and forth the tab the images suddenly appear . in the options argument i am passing SDWebImageRetryFailed.

Any idea would be more than welcome , thanks.

    @interface NeobuggyFrontPageViewController()
    {
        UIImageView *adBanner;
        NSArray *news ;
        NSMutableData *imageData;
        NSURLConnection *connection;
        UIImage *neoImage;
        NSString *category;
        RSSItem *item;
        FrontPageViewNewsCell *cell ;
        CGRect newFrame;
        __block CGRect descFrame;
        float newWidth , newHeight;
         __block float blockHeight;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    item = [news objectAtIndex:[indexPath row]];
    cell = [tableView dequeueReusableCellWithIdentifier:@"FrontPageNewsCell"];
    [[cell newsTitle] setText:[item title]];
    [[cell newsVideo] setAlpha:0.0];
    if ([item newsImageURL]){
        descFrame = [[cell newsBody] frame];

        [[cell newsImage] setImageWithURL:[item newsImageURL] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"] options:SDWebImageRetryFailed  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
            if (image) {
                CGRect newsImageFrame = [[cell newsImage] frame];
                float downImageWidth = image.size.width;
                float downImageHeight = image.size.height;
                float newsImageWidth = 304.00; //imageViewSize.size.width;
                if (downImageWidth > newsImageWidth) {
                    float scaleFactor = newsImageWidth / downImageWidth;
                    newWidth = downImageWidth * scaleFactor;
                    newHeight = downImageHeight * scaleFactor;

                } else {
                    newWidth = downImageWidth;
                    newHeight = downImageHeight;
                }

                newFrame = CGRectMake(newsImageFrame.origin.x, newsImageFrame.origin.y, newWidth, newHeight);

                [[cell newsImage] setFrame:newFrame]; //asigno el nuevo frame a la imagen . 

                float newOriginY = newFrame.origin.y + newFrame.size.height + 10.00;
                descFrame = CGRectMake(cell.newsBody.frame.origin.x,
                                       newOriginY,
                                       304.00,
                                       cell.newsBody.frame.size.height);


                blockHeight = newHeight;
            }
            NSLog(@"image %@ cached ? %u", image ,cacheType);
            if (error) {
                NSLog(@"Error: %@",error);
            }

        }];

    } else if([item newsVideoURL]) {
        [[cell newsImage] setAlpha:0.0];
        [[cell newsVideo] setAlpha:1.0];
        [[cell newsVideo] loadRequest:[NSURLRequest requestWithURL:[item newsVideoURL]]];
        float newOriginY = cell.newsVideo.frame.origin.y + cell.newsVideo.frame.size.height + 10.00;
        descFrame = CGRectMake(cell.newsBody.frame.origin.x, newOriginY, 304, cell.newsBody.frame.size.height);
        blockHeight = cell.newsVideo.frame.size.height;
    }
    [[cell newsImage] setNeedsDisplay];
    NSString *desc = [[NSString alloc] initWithString:[item description]];
    [[cell newsBody] setFrame:descFrame];
    desc = [desc htmlToString:desc];
    [[cell newsBody] setText:desc];
    [[cell newsBody] setNumberOfLines:0];
    [[cell newsBody] sizeToFit];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;

}

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        cell  = (FrontPageViewNewsCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        CGSize descHeight = [[[cell newsBody] text] sizeWithFont:[[cell newsBody] font] constrainedToSize:cell.newsBody.frame.size lineBreakMode:[[cell newsBody] lineBreakMode]];
        CGSize titleHeight = [[[cell newsTitle] text] sizeWithFont:[[cell newsTitle] font] constrainedToSize:cell.newsTitle.frame.size lineBreakMode:[[cell newsTitle] lineBreakMode]];
        newHeight = cell.newsImage.frame.size.height;
        if (blockHeight == 0) {
            [tableView reloadData];
        } else {
            float ch = titleHeight.height + blockHeight + descHeight.height + 70;
            [self setCellHeight:ch];

        }
        return _cellHeight;

    }

Partially solved although solution less than ideal. Since i only needed to show 10 cells I prevented the reuse of the UITableViewCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    item = [news objectAtIndex:[indexPath row]];
    NSString *identifier = [NSString stringWithFormat:@"Cell %d", indexPath.row];
    UINib *nib = [UINib nibWithNibName:@"FrontPageViewNewsCell" bundle:nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:identifier];
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];


    // cell = [tableView dequeueReusableCellWithIdentifier:@"FrontPageNewsCell"];

still interested in finding a solution reusing cell

Thanks,

cromanelli
  • 576
  • 1
  • 5
  • 21
  • Looks like you're missing some code. Where is `descFrame` used after it's set again? And what is `blockHeight` used for? – MishieMoo Apr 18 '13 at 02:07
  • MishieMoo Thanks for the help , i added to the code the instance variable declaration and the cell height method as well. descFrame its a label frame because we have to adjust label position after setting the UIImageView size ... blockheight is the new UIImageView height after the image is downloaded and is used when the cell height is set . – cromanelli Apr 18 '13 at 12:42
  • this problem was probably fixed by the following update: https://github.com/rs/SDWebImage/pull/683 – tt_emrah Mar 19 '15 at 12:27

0 Answers0