我子类化的 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;
}
在我滚动之前一切看起来都很好。任何想法可能是什么原因?
滚动前 向下滚动 向上滚动
最佳答案
问题是单元格中的 subview 没有调整大小。最简单的解决方案(至少对于 UILabels)是将 autoresizingmask 设置为: UIViewAutoresizingFlexibleHeight.
关于ios - 滚动后 UICollectionViewCells subview 呈现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973476/