我有一个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