我試圖通過繼承UICollectionViewFlowLayout來定義UICollectionView的自定義流佈局。我想要的佈局是這樣的:UICollectionViewFlowLayout可變單元格寬度和高度的空白空間
正如你所看到的,我已經分手集合視圖到2個等於行沿水平中間的鴻溝。頂行被分成三份給出3列,底行被分成兩份給出2列。
一切似乎工作,直到您滾動到右側(集合視圖滾動是UICollectionViewScrollDirectionHorizontal)外觀精緻,並看到有一個大的空間後的5個細胞已經完成渲染:
我不知道爲什麼會發生這種情況,但是看起來空白空間與頂部行的單元格寬度相同;集合視圖寬度的三分之一。
下面是我設置的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;
}
任何人都有任何想法,爲什麼我所有的單元格都渲染後,我有大的空白空間?或者沒有人有更好的方法來呈現我之後的佈局。所有的代碼都可以作爲示例XCode項目here。回答,提示,想法,拉請求非常感謝。