mam CollectionViewControllerUtworzenie podklasy UICollectionViewLayout i przypisać do UICollectionView
- (void)viewDidLoad
{
[super viewDidLoad];
// assign layout (subclassed below)
self.collectionView.collectionViewLayout = [[CustomCollectionLayout alloc] init];
}
// data source is working, here's what matters:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ThumbCell *cell = (ThumbCell *)[cv dequeueReusableCellWithReuseIdentifier:@"ThumbCell" forIndexPath:indexPath];
return cell;
}
Mam też UICollectionViewLayout podklasy: CustomCollectionLayout.m
#pragma mark - Overriden methods
- (CGSize)collectionViewContentSize
{
return CGSizeMake(320, 480);
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return [[super layoutAttributesForElementsInRect:rect] mutableCopy];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// configure CellAttributes
cellAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
// random position
int xRand = arc4random() % 320;
int yRand = arc4random() % 460;
cellAttributes.frame = CGRectMake(xRand, yRand, 150, 170);
return cellAttributes;
}
Próbuję użyć nową ramkę do komórki, tylko jeden w przypadkowej pozycji. Problem polega na tym, że layoutAttributesForItemAtIndexPath nie jest wywoływany.
Z góry dziękuję za radę.
Czy ktoś wie, gdzie można znaleźć próbki realizacji niestandardową UICollectionViewLayout? – brainondev
Możesz spojrzeć na mój inspirowany przez Pinterest UICollectionViewLayout na https://github.com/jayslu/JSPintDemo –
Naprawdę interesujący Jay ... dzięki za udostępnienie! – brainondev