Gui*_*iks 11 cocoa-touch nsfetchedresultscontroller ios6 uicollectionview
我已经实现了UICollectionView一个自定义布局.它为布局添加了装饰视图.我使用以下代码添加装饰视图的布局属性:
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *allAttributes = [super layoutAttributesForElementsInRect:rect];
return [allAttributes arrayByAddingObject:[self layoutAttributesForDecorationViewOfKind:kHeaderKind atIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
}
Run Code Online (Sandbox Code Playgroud)
集合视图中的数据由a提供NSFetchedResultsController.
现在它看起来好像它工作正常,但是当集合视图为空时,它失败,因为有第0节.试图在没有索引路径的情况下使用它,但也失败了.关于如何在空中使用装饰视图的任何想法UICollectionView?应该是可能的,因为装饰视图不是数据驱动的.
我创建并测试了这个简单的示例,它似乎可以在 iOS 7 中的所有可能情况下运行(0 个部分、1 个部分有 0 个项目等)。这是我的布局类,UICollectionViewFlowLayout. 该项目的其余部分只是脚手架。
#import "JKLayout.h"\n#import "JKDecoration.h"\n\n@implementation JKLayout\n\n- (instancetype)init\n{\n if (self = [super init]) {\n [self registerClass:[JKDecoration class] forDecorationViewOfKind:@"Decoration"];\n }\n return self;\n}\n\n- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect\n{\n NSArray *allAttributes = [super layoutAttributesForElementsInRect:rect];\n\n // It\xe2\x80\x99s important to set indexPath to nil. If I had set it to indexPath 0-0, it crashed with InternalInconsistencyException\n // because I was trying to get decoration view for section 0 while there in reality was no section 0\n // I guess if you need to have several decoration views in this case, you\xe2\x80\x99d identify them with a method other than indexpath\n return [allAttributes arrayByAddingObject:[self layoutAttributesForDecorationViewOfKind:@"Decoration" atIndexPath:nil]];\n}\n\n- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath\n{\n UICollectionViewLayoutAttributes *attr = [super layoutAttributesForDecorationViewOfKind:decorationViewKind atIndexPath:indexPath];\n if (!attr) {\n attr = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];\n attr.frame = CGRectMake(0, 200, 100, 100);\n }\n return attr;\n}\n\n@end\nRun Code Online (Sandbox Code Playgroud)\n