SDWebImage不会在UITableViewCell中随机显示

时间:2013-04-18 00:20:41

标签: objective-c objective-c-blocks sdwebimage

你好我有一个下载图像的脚本,并在完成下载后动态设置UITableView高度,但有些图像随机显示,好奇的是,当我来回切换标签时,图像会突然出现。在options参数中,我传递了SDWebImageRetryFailed。

任何想法都会受到欢迎,谢谢。

    @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;

    }

部分解决了虽然解决方案不够理想。因为我只需要显示10个单元格,所以我阻止了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"];

仍然有兴趣找到重用细胞的解决方案

谢谢,

0 个答案:

没有答案