我子类化的 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;
}
在我滚动之前,一切看起来都很好。任何想法可能是什么原因?
滚动之前 向下 滚动 向上 滚动