के साथ रिक्त स्थान खाली होने पर मैं UICollectionViewFlowLayout subclassing द्वारा UICollectionView के लिए कस्टम प्रवाह लेआउट को परिभाषित करने का प्रयास कर रहा हूं। लेआउट मैं चाहता हूँ इस तरह दिखता है:UICollectionViewFlow वैरिएबल सेल चौड़ाई और ऊंचाई
आप देख सकते हैं, मैं क्षैतिज बीच विभाजन के साथ 2 बराबर पंक्तियों में संग्रह को देखने विभाजित किया है। शीर्ष पंक्ति को 3 कॉलम देने के लिए तीसरे स्थान पर विभाजित किया गया है, और नीचे पंक्तियां 2 कॉलम देने के लिए आधे हिस्से में विभाजित होती हैं।
:सब कुछ के बाद 5 कोशिकाओं प्रदान की जा रही समाप्त कर दिया है काम करते हैं और जब तक आप (संग्रह दृश्य स्क्रॉल UICollectionViewScrollDirectionHorizontal है) सही करने के लिए स्क्रॉल ठीक लग, और वहाँ एक बड़ी जगह को देखने के लिए लगता है
मुझे नहीं पता कि यह क्यों हो रहा है, हालांकि ऐसा लगता है कि रिक्त स्थान शीर्ष पंक्ति से एक सेल के समान चौड़ाई के बारे में दिखता है; संग्रह दृश्य की चौड़ाई का एक तिहाई।
:यहाँ कैसे मैं सेटअप UICollectionView और UICollectionViewFlowLayout उपवर्ग
- (void)loadView {
[super loadView];
[self setupCollectionView];
}
- (void)setupCollectionView {
CustomFlowLayout *flowLayout = [[CustomFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumInteritemSpacing = 0.0;
flowLayout.minimumLineSpacing = 0.0f;
CGRect frame = CGRectMake(0, 0, 748, 1024);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellID];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.pagingEnabled = YES;
self.collectionView = collectionView;
[self.view addSubview:self.collectionView];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 5;
}
संग्रह दृश्य में वस्तुओं के आकार दिए गए सेक्शन के लिए लेआउट विन्यास द्वारा परिभाषित किया गया है, तो मैं तो जैसे collectionView:layout:sizeForItemAtIndexPath:
लागू है
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath].size;
}
UICollectionViewLayout की मेरे कस्टम उपवर्ग के रूप में कार्यान्वित किया जाता है इस प्रकार है:
@implementation CustomFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
if (!currentItemAttributes) {
currentItemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
}
float height = self.collectionView.frame.size.height;
float width = self.collectionView.frame.size.width;
CGRect frame = CGRectZero;
switch (indexPath.row) {
case 0:
frame.size = CGSizeMake(width/3, height/2);
frame.origin = CGPointMake(0, 0);
break;
case 1:
frame.size = CGSizeMake(width/3, height/2);
frame.origin = CGPointMake(width/3, 0);
break;
case 2:
frame.size = CGSizeMake(width/3, height/2);
frame.origin = CGPointMake(2*width/3, 0);
break;
case 3:
frame.size = CGSizeMake(width/2, height/2);
frame.origin = CGPointMake(0, height/2);
break;
case 4:
frame.size = CGSizeMake(width/2, height/2);
frame.origin = CGPointMake(width/2, height/2);
break;
default:
break;
}
currentItemAttributes.frame = frame;
currentItemAttributes.size = frame.size;
return currentItemAttributes;
}
किसी को भी कोई विचार है कि सभी कोशिकाओं को प्रस्तुत करने के बाद मेरे पास बड़ी खाली जगह क्यों है? या क्या मेरे पास लेआउट को प्रस्तुत करने के लिए कोई बेहतर दृष्टिकोण है। सभी कोड एक नमूना एक्सकोड परियोजना here के रूप में उपलब्ध है। उत्तर, टिप्स, विचार, पुल अनुरोधों की बहुत सराहना की जाती है।