Tôi có một CollectionViewControllersubclassing UICollectionViewLayout và gán cho 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;
}
Tôi cũng có một lớp con 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;
}
Tôi đang cố gắng sử dụng một khung mới cho các tế bào, chỉ một ở một vị trí ngẫu nhiên. Vấn đề là layoutAttributesForItemAtIndexPath không được gọi.
Cảm ơn trước vì lời khuyên nào.
Có ai biết nơi để tìm thấy một ví dụ thực hiện tùy chỉnh cho UICollectionViewLayout? – brainondev
Bạn có thể xem UICollectionViewLayout lấy cảm hứng từ Pinterest của tôi tại https://github.com/jayslu/JSPintDemo –
Jay thực sự thú vị ... cảm ơn bạn đã chia sẻ! – brainondev