Image in UITableView Cell not showing until the cell re used

时间:2016-12-09 12:41:06

标签: ios objective-c uitableview

I am showing an image in a UITableViewCell. When I am entering the screen, the image does not show but after scrolling, e.g resuing the cell the image shows up. The problem is shown below.

enter image description here

The code I have used is

{
    EMROUploadImageCell *cell = (EMROUploadImageCell *)[tableView dequeueReusableCellWithIdentifier:@"EMROUploadImageCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (![[dictInput valueForKey:@"image"]isEqualToString:@""])
    {
        cell.imageViewUploadImage.layer.borderWidth = 0.0;
        cell.imageViewUploadImage.layer.cornerRadius = cell.imageViewUploadImage.frame.size.height/2;
        cell.imageViewEdit.hidden = YES;
        cell.imageViewUploadImage.clipsToBounds = YES;
        if (isImageChanged)
        {
            NSData *data = [NSData dataWithContentsOfFile:[dictInput valueForKey:@"image"]];
            UIImage *image = [UIImage imageWithData:data];
            [cell.imageViewUploadImage setImage:image];
        }
        else
        {
            [cell.imageViewUploadImage sd_setImageWithURL:[NSURL URLWithString:[dictInput valueForKey:@"image"]]
                             placeholderImage:nil
                                      options:SDWebImageRefreshCached];
         }

    }
    else
    {
        cell.imageViewUploadImage.layer.borderWidth = 1.0f;
        cell.imageViewUploadImage.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        cell.imageViewUploadImage.layer.cornerRadius = cell.imageViewUploadImage.frame.size.height/2;
        cell.imageViewEdit.hidden = NO;
        cell.imageViewEdit.clipsToBounds = YES;
        cell.imageViewUploadImage.layer.masksToBounds = YES;
        cell.imageViewUploadImage.clipsToBounds = YES;
        [cell.imageViewUploadImage setNeedsDisplay];
        [cell.imageViewUploadImage setNeedsLayout];
        [cell.imageViewUploadImage layoutIfNeeded];
    }
    [cell.btnUploadImage addTarget:self action:@selector(btnUploadImageAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

3 个答案:

答案 0 :(得分:0)

Add

[tableView reloadData];

into btnUploadImageAction: method.

答案 1 :(得分:0)

在主线程上重新加载表,我之前遇到过同样的问题,

dispatch_async(dispatch_get_main_queue(), ^{
    [tableView reloadData];
});

希望有所帮助!

答案 2 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EMROUploadImageCell *cell = (EMROUploadImageCell *)[tableView dequeueReusableCellWithIdentifier:@"EMROUploadImageCell"];

    [tableView beginUpdates];
    // your code
    [tableView endUpdates];

    return cell;
}

希望有所帮助!