【问题标题】:UICollectionViewCell rendering issues with scrollUICollectionViewCell 滚动渲染问题
【发布时间】:2014-07-19 08:46:05
【问题描述】:

我有一个 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 笔尖中,我有两个 UIView,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,这似乎有帮助,但并不能完全解决问题。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

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

    [cell setNeedsDisplay];
    

    到您的 cellForItemAtIndexPath 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2015-01-27
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      相关资源
      最近更新 更多