मैं एक कस्टम लेआउट के साथ एक UICollectionView का उपयोग कर रहा हूं जो एक ग्रिड प्रारूप में कोशिकाओं को बताता है। 50 से अधिक पंक्तियों और 50 कॉलम अच्छी तरह से हो सकते हैं। स्क्रॉलिंग लंबवत और क्षैतिज दोनों होता है। वर्तमान में, मैं prepareLayout
में लेआउट सेटअप के सभी कर रहा हूँ और सरणियों में भंडारण:UICollectionView बहुत धीमी कस्टम लेआउट
- (void)prepareLayout {
NSMutableArray *newLayoutInfo = [[NSMutableArray alloc] init];
NSMutableArray *newLinearLayoutInfor = [[NSMutableArray alloc] init];
NSInteger sectionCount = [self.collectionView numberOfSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
self.heightForRows = [delegate collectionViewHeightForAllRows];
self.totalWidthsForRows = [[NSMutableArray alloc] init];
for (int i = 0; i < sectionCount; i++) {
[self.totalWidthsForRows addObject:[NSNumber numberWithInt:0]];
}
for (NSInteger section = 0; section < sectionCount; section++) {
NSMutableArray *cellLayoutInfo = [[NSMutableArray alloc] init];
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 frameForCellAtIndexPath:indexPath];
[cellLayoutInfo addObject:itemAttributes];
[newLinearLayoutInfor addObject:itemAttributes];
}
[newLayoutInfo addObject:cellLayoutInfo];
}
self.layoutInfo = newLayoutInfo;
self.linearLayoutInfo = newLinearLayoutInfor;
}
फिर
layoutAttributesForElementsInRect
में
मेरे पास है:
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *rows = [self.linearLayoutInfo filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
return CGRectIntersectsRect(rect, [evaluatedObject frame]);
}]];
यह ठीक काम करता है, लेकिन यह laggy और चिड़चिड़ा है जब मैं 50 से अधिक कॉलम और 50 पंक्तियां हैं। समस्या मैं अब है कि मैं
-(BOOL)shouldInvalidateLayoutForBoundsChange {
return YES;
}
यह पूरे लेआउट हर बार सीमा परिवर्तन है, जो, कहने की प्रदर्शन पर बहुत बड़ा प्रभाव पड़ता है तैयार करता है सेट करना होगा और आप मुश्किल से स्क्रॉल कर सकते हैं है। कोशिकाओं में केवल एक अपारदर्शी पृष्ठभूमि के साथ पाठ होता है, इसलिए वहां कोई समस्या नहीं है।
मुझे यकीन है कि मैं यह सही नहीं कर रहा हूं और यह एक बेहतर तरीका होना चाहिए। अग्रिम में मदद के लिए धन्यवाद।
आप देखें कि आप कहां स्क्रॉल के दौरान अपने समय खर्च कर रहे हैं उपकरण का उपयोग करना चाहिए। – nielsbot
मैं तत्वों को बिछाने की अनुशंसा करता हूं क्योंकि वे आपके संग्रह दृश्य में जोड़े गए हैं। लेआउट तत्वों की कोई आवश्यकता नहीं है जो पहले से ही हैं - वे सही स्थिति में हैं। यदि आप इसे इस तरह से करते हैं तो आप 'चाहिएInvalidateLayoutForBoundsChange' – nielsbot
@nielsbot को बंद कर सकते हैं मेरे पास पूरक विचार हैं जो हेडर के रूप में कार्य कर रहे हैं जिन्हें हमेशा संग्रहदृश्य की सामग्री ऑफ़सेट पर सेट किया जाना चाहिए। ये दृश्य के शीर्ष और किनारे पर हैं। अगर मैं 'चाहिएइविलिडेट लेटआउटफॉरबाउंड चेंज' की बारी करता हूं तो हेडर अब "फ्लोट" नहीं करते हैं जहां उन्हें माना जाता है। क्या इसका कोई अलग समाधान है? –