简体   繁体   English

在重新使用单元格之前,UITableViewCell中的UIImageView不会显示

[英]UIImageView in UITableViewCell not showing up until cell is being reused

I am having a weird issue with a custom UITableViewCell implementation that I have: 我有一个自定义UITableViewCell实现的怪异问题:

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
    [cell setupCellViewWithSavedStory:myStory];

    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithPulseStyle:PNRTableViewCellAccessoryDefault reuseIdentifier:@"SavedStories"];
    if (self) {

        storyImageView_ = [[UIImageView alloc] initWithFrame:CGRectMake(kPadding, kPadding, kImageSize, kImageSize)];
        storyImageView_.layer.borderWidth = 2.0;
        storyImageView_.layer.borderColor = [UIColor whiteColor].CGColor;

        storyTitleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyImageView_.frameRight + kPadding*1.5, storyImageView_.frameY, self.bounds.size.width - storyImageView_.frameWidth - 4*kPadding, 0)];
        storyTitleLabel_.numberOfLines = 0;
        storyTitleLabel_.lineBreakMode = UILineBreakModeClip;
        storyTitleLabel_.font = [UIFont fontWithName:kProximaNovaBold size:14];
        storyTitleLabel_.textColor = [UIColor whiteColor];
        storyTitleLabel_.backgroundColor = [UIColor clearColor];

        storyPublisherLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyTitleLabel_.frameX, storyTitleLabel_.frameHeight, storyTitleLabel_.frameWidth, 50)];
        storyPublisherLabel_.numberOfLines = 1;
        storyPublisherLabel_.font = [UIFont fontWithName:kProximaNova size:11];
        storyPublisherLabel_.textColor = [UIColor colorWithWhite:140/255.f alpha:1.0];
        storyPublisherLabel_.backgroundColor = [UIColor clearColor];

        [self.contentView addSubview:storyImageView_];
        [self.contentView addSubview:storyTitleLabel_];
        [self.contentView addSubview:storyPublisherLabel_];

    }
    return self;
}

-(void)setupCellViewWithSavedStory:(MyStory *) myStory
{
    if (myStory.imageState == StoryImageAvailableOnDisk) {
        storyImageView_.hidden = NO;
        storyImageView_.image = myStory.image;
    }
    else {
        storyImageView_.image = nil;
        storyImageView_.hidden = YES;
    }

    CGSize titleSize = [myStory.title sizeWithFont:storyTitleLabel_.font constrainedToSize:CGSizeMake(storyTitleLabel_.frameWidth,  kImageSize - kPadding) lineBreakMode:storyTitleLabel_.lineBreakMode];
    [storyTitleLabel_ setFrameHeight:titleSize.height];
    [storyTitleLabel_ setText:savedStory.title];
    [storyPublisherLabel_ setText:savedStory.domain];
    [self setNeedsLayout];
}

But for some reason is that I can't see image until I scroll down and scroll back up again. 但是由于某些原因,直到向下滚动并再次向上滚动才能看到图像。 In other words the image is reused then I can see the image. 换句话说,图像被重用,然后我可以看到图像。 All of the label text is showing just fine, it's just the image. 所有标签文本显示都很好,只是图像。 Any idea why? 知道为什么吗?

Have you tried this? 你有尝试过吗?

if (cell == nil) {
            cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
           [cell setupCellViewWithSavedStory:myStory];
     }

Try adding this code: 尝试添加以下代码:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
    [cell setupCellViewWithSavedStory:myStory];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM