Ios UICollectionViewCell滚动呈现问题

Ios UICollectionViewCell滚动呈现问题,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我有一个UICollectionView,其中的行高度可变。它对前四个单元格的渲染效果良好,但滚动时,新单元格的尺寸和前几行的数据错误 该单元由两个UIView设置—headerView和entryView。基于标志隐藏或显示标题视图;入口视图可以是A或B,它们具有不同的高度 以下是UICollectionView的委托方法: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForIt

我有一个UICollectionView,其中的行高度可变。它对前四个单元格的渲染效果良好,但滚动时,新单元格的尺寸和前几行的数据错误

该单元由两个UIView设置—headerView和entryView。基于标志隐藏或显示标题视图;入口视图可以是A或B,它们具有不同的高度

以下是UICollectionView的委托方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell"
                                                                 forIndexPath:indexPath];
    Entry *entry = self.entries[indexPath.row];
    [cell setEntry:entry showHeader:showHeader];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = [self cellHeight:self.entries[indexPath.row]];
    return size;
}
在MyCell nib中,我有两个UIView,headerView和entryView。entryView的内容会根据类型进行更改。同样基于showHeader标志,我显示/隐藏header视图

[ UIView - header view ]
[ UIView - entry view ]
在MyCell实现中

@interface ActivityCell ()
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *entryView;
@end

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UINib *nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
        NSArray *objects = [nib instantiateWithOwner:self options:nil];
        [self.contentView addSubview:objects[0]];
    }
    return self;
}

- (void)setEntry:(Entry *)entry {
    _entry = entry;

    if (self.entry.entryType == TypeA) {
        EntryAView *entryA = [[EntryAView alloc] init];
        entryA.entry = entry;
        [self.entryView addSubview:entryA];
    } else if (self.entry.entryType == TypeB) {
        EntryBView *entryB = [[EntryBView alloc] init];
        entryB.entry = entry;
        [self.entryView addSubview:entryB];
    }
}

- (void)setEntry:(Entry *)entry showHeader:(BOOL)showHeader {
    if (showHeader) {
        UserHeader *userHeader = [[UserHeader alloc] init];
        userHeader.user = entry.user;
        [self.userView addSubview:userHeader];
        self.userView.hidden = NO;
        self.topConstraint.constant = UserHeaderHeight;
    } else {
        self.userView.hidden = YES;
        self.topConstraint.constant = 0;
    }
    self.entry = entry;
}

- (void)prepareForReuse {
    [super prepareForReuse];

    NSArray *userViewsToRemove = [self.userView subviews];
    for (UIView *subview in userViewsToRemove) {
        [subview removeFromSuperview];
    }

    NSArray *entryViewsToRemove = [self.entryView subviews];
    for (UIView *subview in entryViewsToRemove) {
        [subview removeFromSuperview];
    }

    self.topConstraint.constant = 0;
    self.entry = nil;
}
我在计算cellHeight中单元格的高度,它返回正确的CGSize。 经过研究,我在单元格中添加了prepareForReuse,这似乎有帮助,但并不能完全解决问题。
这可能是一个并发问题。尝试添加:

[cell setNeedsDisplay];
到您的cellForItemAtIndexPath方法