滚动后UICollectionViewCells子视图呈现错误

时间:2012-12-20 13:41:51

标签: 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

1 个答案:

答案 0 :(得分:1)

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