gpt4 book ai didi

ios - 未使用自定义 UICollectionViewLayout 调用 cellForItemAtIndexPath

转载 作者:行者123 更新时间:2023-12-01 22:18:20 25 4
gpt4 key购买 nike

这就是我初始化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];

比我有以下委托(delegate)方法:
 - (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 1items 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 子类化

最佳答案

几点观察:

  • 关键问题是 layoutAttributesForElementsInRect应该为特定矩形中可见的所有单元格返回一组布局属性。如果您不返回属性对象数组,每个可见单元格一个,它不会询问 cellForItemAt生成任何单元格。

    现在,您将如何为您的特定布局执行此操作可能会有所不同,但例如,这会找到所有与 rect 相交的布局。 , 调用layoutAttributesForItemAtIndexPath对于其中的每一个并将它们附加到数组中。
  • 您的 layoutAttributesForItemAtIndexPath指定所有单元格的中心应位于 Collection View 中的 100、100,并且大小为 10、10。即简而言之,您指定它们都在完全相同的位置,彼此重叠。这有点好奇。通常每个单元格都有一些独特的 center .

    如果您希望它们在 50 x 50 网格中,您可以这样做:
    //  CustomLayout.m

    #import "CustomLayout.h"

    static const CGSize kCellSize = { 70, 30 }; // make whatever size you want
    static const CGFloat kCellSpacing = 5;
    static const NSInteger kCellsPerRow = 50;

    @implementation CustomLayout

    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return true;
    }

    - (CGSize)collectionViewContentSize {
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    if (count == 0) return CGSizeZero;

    NSInteger numberOfColumns = MIN(kCellsPerRow, count);
    NSInteger numberOfRows = (count - 1) % kCellsPerRow + 1;
    return CGSizeMake(numberOfColumns * (kCellSize.width + kCellSpacing) + kCellSpacing,
    numberOfRows * (kCellSize.height + kCellSpacing) + kCellSpacing);
    }

    - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray <UICollectionViewLayoutAttributes *> *attributes = [NSMutableArray array];

    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    NSInteger numberOfRows = (count - 1) % kCellsPerRow + 1;

    NSInteger startColumn = MAX(0, floorf(rect.origin.x / (kCellSize.width + kCellSpacing)));
    NSInteger endColumn = MIN(kCellsPerRow, ceilf((rect.origin.x + rect.size.width) / (kCellSize.width + kCellSpacing)));

    NSInteger startRow = MAX(0, floorf(rect.origin.y / (kCellSize.height + kCellSpacing)));
    NSInteger endRow = MIN(numberOfRows, ceilf((rect.origin.y + rect.size.height) / (kCellSize.height + kCellSpacing)));

    for (NSInteger row = startRow; row < endRow; row++) {
    for (NSInteger column = startColumn; column < endColumn; column++) {
    NSInteger index = row * kCellsPerRow + column;
    if (index < count) {
    [attributes addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]];
    }
    }
    }

    return attributes;
    }

    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    NSInteger column = indexPath.item % kCellsPerRow;
    NSInteger row = indexPath.item / kCellsPerRow;

    attributes.size = kCellSize;
    attributes.center = CGPointMake(column * (kCellSize.width + kCellSpacing) + kCellSpacing + kCellSize.width / 2,
    row * (kCellSize.height + kCellSpacing) + kCellSpacing + kCellSize.height / 2);

    return attributes;
    }

    @end

    显然,如果您想要一个 50 x 50 的网格,那么您可能希望单元格的数量为 2,500,而不是 40。
  • 为了简洁起见,我确信它被省略了,但是您的代码片段没有演示 Collection View 的 collectionViewLayout 的设置。 .

  • 无论如何,上面呈现了以下 Collection View :

    enter image description here

    现在,我将单元格做得足够大,这样我就可以在里面放一个标签并显示单元格的 indexPath.item。 ,但可以随意调整大小。

    关于ios - 未使用自定义 UICollectionViewLayout 调用 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712990/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com