0

您好,我有一个脚本可以下载图像并在完成下载后动态设置 UITableView 高度,但有些图像随机不显示,奇怪的是当我来回切换选项卡时,图像突然出现。在选项参数中,我传递了 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"];

仍然有兴趣找到重用单元的解决方案

谢谢,

4

0 回答 0