gpt4 book ai didi

ios - UICollectionViewLayout。我的所有元素在哪里?

转载 作者:行者123 更新时间:2023-11-29 03:46:26 25 4
gpt4 key购买 nike

当我在没有自定义布局对象的情况下实现 UICollectionView 时,所有 16 个可重用单元格都会出现。也就是说,我有 1 个部分,该部分中有 16 个项目。当我创建自定义布局对象时,仅显示 1 个单元格。如果我将节数更改为 5,则会出现 5 个单元格。为什么collectionView在使用布局对象时绘制部分,而在使用默认网格时绘制项目???我错过了什么吗?

我在这里子类化 UICollectionViewLayout:

 static NSString * const kPadLayoutType = @"gridPads";

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
[self setup];
}
return self;
}

- (void)setup
{
self.itemInsets = UIEdgeInsetsMake(22.0f, 22.0f, 13.0f, 22.0f);
self.itemSize = CGSizeMake(125.0f, 125.0f);
self.interItemSpacingY = 12.0f;
self.numberOfColumns = 4;
}

# pragma mark _____Layout

- (void)prepareLayout
{
NSMutableDictionary *newLayoutInfo = [NSMutableDictionary dictionary];
NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];

NSInteger sectionCount = [self.collectionView numberOfSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

for(NSInteger section = 0; section < sectionCount; section++) {
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];

for(NSInteger item = 0; item < itemCount; item++) {
indexPath = [NSIndexPath indexPathForItem:item inSection:section];

UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
itemAttributes.frame = [self frameForPadAtIndexPath:indexPath];

cellLayoutInfo[indexPath] = itemAttributes;
}
}
newLayoutInfo[kPadLayoutType] = cellLayoutInfo;
self.layoutInfo = newLayoutInfo;
}

- (CGSize)collectionViewContentSize
{
return self.collectionView.frame.size;
}

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];

[self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
NSDictionary *elementsInfo,
BOOL *stop) {
[elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
UICollectionViewLayoutAttributes *attributes,
BOOL *innerStop) {
if(CGRectIntersectsRect(rect, attributes.frame)) {
[allAttributes addObject:attributes];
}
}];
}];
return allAttributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.layoutInfo[kPadLayoutType][indexPath];
}

# pragma mark _____Private

- (CGRect)frameForPadAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = indexPath.section/ self.numberOfColumns;
NSInteger column = indexPath.section % self.numberOfColumns;

CGFloat spacingX = self.collectionView.bounds.size.width - self.itemInsets.left - self.itemInsets.right - (self.numberOfColumns * self.itemSize.width);

if(self.numberOfColumns > 1) spacingX = spacingX / (self.numberOfColumns - 1);

CGFloat originX = floorf(self.itemInsets.left + (self.itemSize.width + spacingX) * column);

CGFloat originY = floor(self.itemInsets.top + (self.itemSize.height + self.interItemSpacingY) * row);

return CGRectMake(originX, originY, self.itemSize.width, self.itemSize.height);
}

我使每个 setter 中的布局无效。

我在 UIViewController 中使用 collectionView在 viewController 中,我注册了我的 Cell 类以在 viewDidLoad 中使用并设置以下内容:

 # pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 16;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Pad *myPad = [collectionView dequeueReusableCellWithReuseIdentifier:kPadLayoutType forIndexPath:indexPath];
return myPad;
}

最佳答案

在我看来,您的 frameForPadAtIndexPath: 方法将为同一部分中的每个项目返回相同的框架,并且仅随该部分改变位置。

改变

 NSInteger row = indexPath.section/ self.numberOfColumns;
NSInteger column = indexPath.section % self.numberOfColumns;

进入

 NSInteger row = indexPath.item/ self.numberOfColumns;
NSInteger column = indexPath.item % self.numberOfColumns;

可能会给你更好的结果。

关于ios - UICollectionViewLayout。我的所有元素在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701482/

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