【问题标题】:UICollectionViewCells subview rendered wrong after scrolling滚动后 UICollectionViewCells 子视图呈现错误
【发布时间】:2012-12-20 13:41:50
【问题描述】:

我子类化的 CollectionViewCell 只包含一个 UILabel。 为了获得网格,我将带有 CGRectInset 的 UILabel 添加到单元格中。

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor orangeColor];
        self.label = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 1.0, 1.0)];
        self.label.numberOfLines = 0;
        self.label.layer.shouldRasterize = true;
        [self addSubview:self.label];
    }
    return self;
}

根据要求

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"indexPathSection: %i in row: %i", indexPath.section, indexPath.row);
    MyCVCell *cell = (MyCVCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    NSString *str = [NSString stringWithFormat:@"%@", [[self.cData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    cell.label.text = str;

    return cell;
}

在我滚动之前,一切看起来都很好。任何想法可能是什么原因?

滚动前 向下滚动 向上滚动

【问题讨论】:

  • 请从cellForItemAtIndexPath发布您的代码。
  • @0x7fffffff 将其添加到问题中
  • 尝试将您的子视图添加到单元格 contentView 而不是直接添加到单元格中?
  • @wattson12 试过了,没有任何改变
  • 你有没有解决过这个问题,或者至少想出了一些对你有用的东西?

标签: ios ipad uicollectionview uicollectionviewcell


【解决方案1】:

问题是单元格中的子视图没有调整大小。最简单的解决方案(至少对于 UILabels)是将 autoresizingmask 设置为: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight。

【讨论】:

  • 啊谢谢白毫。我希望你有一个自动布局的答案,但无论如何很高兴看到你解决了它。
猜你喜欢
  • 2019-10-22
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多