gpt4 book ai didi

cocoa-touch - 空 Collection View 中的 UICollectionView 装饰

转载 作者:行者123 更新时间:2023-12-04 02:23:17 25 4
gpt4 key购买 nike

我已经实现了一个 UICollectionView带有自定义布局。它为布局添加了装饰 View 。我使用以下代码添加装饰 View 的布局属性:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *allAttributes = [super layoutAttributesForElementsInRect:rect];
return [allAttributes arrayByAddingObject:[self layoutAttributesForDecorationViewOfKind:kHeaderKind atIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
}

集合 View 中的数据由 NSFetchedResultsController 提供.

现在看起来它工作正常,但是当集合 View 为空时,它会失败,因为有第 0 部分。尝试在没有索引路径的情况下使用它,但也失败了。关于如何在空的 UICollectionView 中使用装饰 View 的任何想法?应该是可能的,因为装饰 View 不是数据驱动的。

最佳答案

我创建并测试了这个简单的例子,它似乎在所有可能的情况下都适用于 iOS 7(0 个部分,1 个部分有 0 个项目等)。这是我的布局类,UICollectionViewFlowLayout 的子类.该项目的其余部分只是脚手架。

#import "JKLayout.h"
#import "JKDecoration.h"

@implementation JKLayout

- (instancetype)init
{
if (self = [super init]) {
[self registerClass:[JKDecoration class] forDecorationViewOfKind:@"Decoration"];
}
return self;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *allAttributes = [super layoutAttributesForElementsInRect:rect];

// It’s important to set indexPath to nil. If I had set it to indexPath 0-0, it crashed with InternalInconsistencyException
// because I was trying to get decoration view for section 0 while there in reality was no section 0
// I guess if you need to have several decoration views in this case, you’d identify them with a method other than indexpath
return [allAttributes arrayByAddingObject:[self layoutAttributesForDecorationViewOfKind:@"Decoration" atIndexPath:nil]];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attr = [super layoutAttributesForDecorationViewOfKind:decorationViewKind atIndexPath:indexPath];
if (!attr) {
attr = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
attr.frame = CGRectMake(0, 200, 100, 100);
}
return attr;
}

@end

关于cocoa-touch - 空 Collection View 中的 UICollectionView 装饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860131/

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