Вот как я инициализирую collectionView:
self.collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:[[customLayout alloc]init]];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
//[self.collectionView setAlwaysBounceVertical:YES];
[self.collectionView setBackgroundColor:[UIColor blueColor]];
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
UINib *nibf = [UINib nibWithNibName:@"inviteUserCell" bundle:nil];
[self.collectionView registerNib:nibf forCellWithReuseIdentifier:@"user"];
[self addSubview:self.collectionView];
Чем у меня есть следующие методы делегата:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
NSLog(@"sections 1");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"items 40");
return 40;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
inviteUserCell *cell = (inviteUserCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"user" forIndexPath:indexPath];
[cell setBackgroundColor:[UIColor whiteColor]];
NSLog(@"CELL: %@", cell);
return cell;
}
В моем журнале я получаю sections 1
и items 40
, но не получаю CELL: %@
. Для моего подкласса UICollectionViewLayout
у меня есть следующие методы.
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return true;
}
- (CGSize)collectionViewContentSize{
CGSize size = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
return size;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
NSArray *attributesInRect = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *cellAttributes in attributesInRect) {
// [self modifyLayoutAttributes:cellAttributes];
cellAttributes.size = CGSizeMake(10, 10);
cellAttributes.center = CGPointMake(100, 100);
}
return attributesInRect;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
// [self modifyLayoutAttributes:attributes];
attributes.size = CGSizeMake(10, 10);
attributes.center = CGPointMake(100, 100);
NSLog(@"Attr: %@", attributes); // this is null
return attributes;
}
Я не уверен, что я не вызываю или не устанавливаю, что вызовет cellForItemAtIndexPath
.
Макет
Желаемый макет — сетка 50x50 с вертикальной и горизонтальной прокруткой. Из чтения вокруг UICollectionViewFlowLayout
возможен только вертикальный или горизонтальный скроллинг. Итак, я создал подкласс UICollectionViewLayout
Вы создаете подклассы
UICollectionViewFlowLayout
илиUICollectionViewLayout
?«Желаемый макет представляет собой сетку 50x50 с вертикальной и горизонтальной прокруткой. Из чтения вокруг
UICollectionViewFlowLayout
возможен только вертикальный или горизонтальный скроллинг. Поэтому я создал подклассUICollectionViewLayout
». ... Да, это правильно. Для этого вам понадобится подклассUICollectionViewLayout
.