我有一個CollectionViewController子類UICollectionViewLayout並分配給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;
}
我也有一個UICollectionViewLayout子類: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;
}
我試圖用一個新的幀的細胞,只有一個隨機位置。 問題是layoutAttributesForItemAtIndexPath未被調用。
在此先感謝您的任何建議。
有誰知道在哪裏找到UICollectionViewLayout自定義實現樣品? – brainondev
你可以在https://github.com/jayslu/JSPintDemo看看我的Pinterest啓發UICollectionViewLayout –
非常有趣的Jay ...感謝分享! – brainondev