ios - 滚动后 UICollectionViewCells subview 呈现错误

标签 ios ipad uicollectionview uicollectionviewcell

我子类化的 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;
}

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

滚动前 views rendered right 向下滚动 scrolling down 向上滚动 scrolling back up

最佳答案

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

关于ios - 滚动后 UICollectionViewCells subview 呈现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973476/

相关文章:

ios - 在 React Native 中获取 View 的大小

ios - NSInternalInconsistencyException - “请求 MSCollectionElementKindCurrentTimeHorizo​​ntalGridline 装饰 View 的布局属性

javascript - iPad 和 jQuery hide() 在页面加载时有很大的延迟

ios - 如何将一个 TableView 放入另一个 TableView 的单元格中?任何想法?

iphone - iOS - 在应用商店加载下载页面?

ios - 下载图像后重新加载 CollectionView 的单元格

ios - 如何在不加载以前的数据的情况下从父 View 中删除和添加 UICollectionView ?

objective-c - 我是否必须发布以这种方式生成的深度复制的 NSMutableDictionary?

ios - 如何为我的应用添加几种语言?不是描述

ios - Obj-c 为静态库导出 .h 文件 - 不想公开类定义