需求:为实现第一行显示一个,第二行以后显示两个
方案1:用系统自带的流布局,实现的效果是,若第二行只有一个,则系统默认会居中显示,不是左对齐(如下图),不符合项目要求。
方案2:自定义系统的UICollectionViewFLowLayout,主要代码如下, 只要设置了cell的LayoutAttribute之后,supplementaryView的,用super的
subclass of UICollectionViewFlowLayout
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
// NSLog(@"_________InRect________");
NSArray* original = [super layoutAttributesForElementsInRect:rect];
NSArray<UICollectionViewLayoutAttributes*>* attributesToReturn = [[NSArray alloc]initWithArray:original copyItems:YES];
for (int i=attributesToReturn.count-1; i>=0; i--) {
if (nil == attributesToReturn[i].representedElementKind) {
NSIndexPath* indexPath = attributesToReturn[i].indexPath;
attributesToReturn[i].frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
else if([attributesToReturn[i].representedElementKind isEqual:UICollectionElementKindSectionHeader]){
// attributesToReturn[i].frame = [self layoutAttributesForSupplementaryViewOfKind:@"HeaderViewOfCollection" atIndexPath:attributesToReturn[i].indexPath].frame;
}
}
return attributesToReturn;
}
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes *currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
CGFloat WIDTH = FirstItemWidth;
CGFloat hight = FirstItemHeight;
NSInteger count = 0;
for (NSInteger i=0; i<=indexPath.section; i++) {
for (NSInteger j =0; j<=indexPath.row; j++) {
if (i==0 && j==0) {
WIDTH = FirstItemWidth;
hight = FirstItemHeight;
}
else{
WIDTH = CommonItemWidth;
hight = CommonItemHeight;
}
CGFloat xPos = 0;
CGFloat yPos = 0;
//Calculate x
if ( (j%2==1 && i>0) || (i==0 && (j-1)%2==1) ) {
xPos = CommonItemWidth+ self.minimumInteritemSpacing;
}
//Calculate y
for (int k=0; k<i; k++) {
if (k==0) {
yPos = FirstItemHeight;
yPos += (int)(list.SectionArray[k].cellArray.count/2) * CommonItemHeight +(list.SectionArray[k].cellArray.count/2)*self.minimumLineSpacing;
}
else{
yPos += (int)((list.SectionArray[k].cellArray.count+1)/2) * CommonItemHeight + ((list.SectionArray[k].cellArray.count-1)/2)*self.minimumLineSpacing;
}
}
if (i==0) {
if (j>0) {
yPos = FirstItemHeight + (j-1)/2*CommonItemHeight + (j+1)/2*self.minimumLineSpacing;
}
}
else {
yPos += j/2*CommonItemHeight + j/2*self.minimumLineSpacing;
}
//设置item的位置
currentItemAttributes.frame = CGRectMake(self.sectionInset.left+xPos, (i+1)*SectionHeaderHeight +yPos,WIDTH, hight);
count++;
}
}
return currentItemAttributes;
}
//数组的相关设置在这个方法中
//布局前的准备会调用这个方法
-(void)prepareLayout{
self.dataModel = [DataModel getSharedInstance];
[super prepareLayout];
if (self.type ==0) {
list = self.wifiConnected ? self.dataModel.JC_List : self.dataModel.JC_ListLocal;
}
else{
list = self.wifiConnected ? self.dataModel.JJ_List : self.dataModel.JJ_ListLocal;
}
self.minimumInteritemSpacing = 10;
self.minimumLineSpacing = 10;
}
UICollectionViewController
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
HeaderViewOfCollection *headerView = nil;
if ([kind isEqual:UICollectionElementKindSectionHeader]) {
// configure headerView
return headerView;
}
return headerView;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(320, 38);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10; //5
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10; //8
}