UICollectionViewCell渲染滚动问题

时间:2014-07-19 08:46:06

标签: ios objective-c uicollectionview uicollectionviewcell

我有一个UICollectionView,其行具有可变高度。它为前四个单元格渲染得很好,但滚动时,新单元格的尺寸和前一行的数据都是错误的。

单元格设置有两个UIViews - 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笔尖中,我有两个UIViews,headerViewentryView。 entryView的内容根据类型而变化。同样基于showHeader标志,我显示/隐藏标题视图。

[ 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,这似乎有所帮助,但并未完全解决问题。

1 个答案:

答案 0 :(得分:0)

这可能是并发问题。尝试添加:

[cell setNeedsDisplay];

cellForItemAtIndexPath 方法。