您好,我有一个脚本,可以下载图像并在完成下载后动态设置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"];

仍然对寻找解决方案重用单元感兴趣

谢谢,

  ask by cromanelli translate from so

本文未有回复,本站智能推荐: