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

时间:2012-09-21 22:28:01

标签: iphone objective-c ios ipad

我遇到了一个我自己的自定义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];
}

但由于某些原因,我向下滚动并再次向上滚动后才能看到图像。换句话说,图像被重用,然后我可以看到图像。所有标签文字都显示得很好,它只是图像。知道为什么吗?

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

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

答案 1 :(得分:0)

尝试添加此代码:

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