The CollectionViewCell i subclassed contains just a UILabel.
To get the grid i add the UILabel with a CGRectInset to the cell.
- (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;
}
As requested
- (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;
}
All looks fine till i scroll. Any ideas what could be the cause?
Before scroll

scrolling down

scrolling up

Problem was that subviews in the cell didn’t resize. Easiest solution (at least for UILabels) is to set autoresizingmask to: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight.