11

मैंने कस्टम लेआउट के साथ UICollectionView लागू किया है। यह लेआउट में एक सजावट दृश्य जोड़ता है। मैं सजावट देखने के लेआउट विशेषताओं को जोड़ने के लिए निम्न कोड का उपयोग करें:रिक्त संग्रह दृश्य में UICollectionView सजावट

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

संग्रह दृश्य में डेटा एक NSFetchedResultsController द्वारा प्रदान की जाती है।

अब ऐसा लगता है कि यह ठीक काम करता है, लेकिन जब संग्रह दृश्य खाली होता है, तो यह विफल रहता है क्योंकि धारा 0 है। इसे इंडेक्स पथ के बिना उपयोग करने का प्रयास किया गया है, लेकिन यह भी विफल रहता है। खाली UICollectionView में सजावट दृश्यों का उपयोग करने के तरीके पर कोई विचार? संभव होना चाहिए क्योंकि सजावट के विचार डेटा संचालित नहीं हैं।

+1

हाय, क्या आपने इस समस्या को हल किया है, कृपया अपना उत्तर अपडेट करें ताकि हम कुछ मदद भी प्राप्त कर सकें। –

+0

क्या आप कुछ और कोड पोस्ट कर सकते हैं ताकि मैं आसानी से समस्या को पुन: उत्पन्न कर सकूं –

+1

क्या आपको दोनों मामलों में एक ही त्रुटि संदेश मिलता है? क्योंकि यह मेरे लिए काम करता है जब मैं इंडेक्स पथ (एक्सकोड 5.1.1, आईओएस एसडीके 7.1 सिम्युलेटर) के रूप में 'शून्य' का उपयोग करता हूं। यदि यह बदले में चुपचाप विफल रहता है, तो शायद 'सुपर' कॉल 'शून्य' ('UICollectionViewLayout का डिफ़ॉल्ट) लौटाता है? –

उत्तर

0

मैंने इस सरल उदाहरण को बनाया और परीक्षण किया जो सभी संभावित परिस्थितियों में आईओएस 7 में काम करता है (0 अनुभाग, 0 आइटम के साथ 1 सेक्शन आदि)। यह मेरा लेआउट क्लास है, 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 
+1

सुनिश्चित नहीं है कि सूचकांक पथ के रूप में 'शून्य' के उपयोग ने चीजों को बदल दिया है, लेकिन उपर्युक्त कोड के साथ यह वास्तव में पूरी तरह से काम करता है। यह भी संभव है कि यह फिर से काम नहीं करता, 'यूआईसीओलेक्शन व्यू फिर थोड़ी छोटी थी। –

+0

हाँ, शायद उपर्युक्त सभी का संयोजन। दस्तावेज़ों ने सजावट के विचारों के लिए NSIndexPath का इलाज करने के तरीके के बारे में अधिक मार्गदर्शन नहीं दिया है, यह कहने के अलावा "यह निर्धारित करने के लिए कि आप दिए गए सजावट दृश्य की पहचान करने के लिए इंडेक्सपैथ पैरामीटर का उपयोग कैसे करें"। – Jaanus

+1

दुर्भाग्य से यह अब तक काम नहीं करता है क्योंकि 'layoutAttributesForDecorationViewOfKind: indexPath: '' nonnull' अनुक्रमणिका पथ की अपेक्षा करता है। – ianolito

4

जब एक सजावट दृश्य या एक पूरक एक विशेष सेल से जुड़ी नहीं दृश्य का उपयोग कर, सूचकांक पथ निर्दिष्ट करने के लिए [NSIndexPath indexPathWithIndex:] का उपयोग करें। यहां एक नमूना कोड है:

@interface BBCollectionViewLayout : UICollectionViewFlowLayout 

@end 

@implementation BBCollectionViewLayout 

- (void)BBCollectionViewLayout_commonInit { 
    [self registerClass:[BBCollectionReusableView class] forDecorationViewOfKind:BBCollectionReusableViewKind]; 
} 

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

- (id)init { 
    self = [super init]; 
    if (self) { 
     [self BBCollectionViewLayout_commonInit]; 
    } 
    return self; 
} 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSMutableArray *array = [NSMutableArray arrayWithArray:[super layoutAttributesForElementsInRect:rect]]; 

    UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForDecorationViewOfKind:BBCollectionReusableViewKind atIndexPath:[NSIndexPath indexPathWithIndex:0]]; 

    if (CGRectIntersectsRect(rect, attributes.frame)) { 
     [array addObject:attributes]; 
    } 

    return array; 
} 

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath { 
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:elementKind withIndexPath:indexPath]; 
    attributes.frame = CGRectMake(0., 60., 44., 44.); 
    return attributes; 
} 

@end 
+0

हाँ, NSIndexPath का उपयोग करके जो अनुभाग/आइटमों पर आधारित नहीं है, काम करता है। मुझे थोड़ा असहज बनाता है-मुझे लगता है कि इनमें से अधिकतर एपीआई-मानते हैं कि इंडेक्सपाथ सेक्शन.इटिम प्रारूप से मेल खाता है, हालांकि इस विशिष्ट उपयोग के किसी भी दुष्प्रभाव का प्रतीत नहीं होता है। – Jaanus

+0

@ जेनस अगर शून्य स्वीकार्य मूल्य था, तो मुझे लगता है कि इसका दस्तावेज़ीकरण में उल्लेख किया जाएगा। इंडेक्स पथ किसी विशिष्ट आइटम से बंधे नहीं होने के बाद से किसी सेक्शन + आइटम के साथ इंडेक्स पथ बनाना सामान्य बात है। – Benoit

संबंधित मुद्दे