999yzc亚洲城在线娱乐城UICollectView的使用问题

( 999yzc亚洲城在线娱乐城 )

我在写一个布局 不知道哪里写错了 找了半天 我对UIcollectionView的使用不是很熟练

代码在此 UIControllerView中的CollectionView

- voidviewDidLoad {
    [super viewDidLoad];
    self.mainView.collectionViewLayout = [[MineCollectionCustomLayout alloc] init];
   
    [self.mainView registerClass:[MineSingleCollectionCell class] forCellWithReuseIdentifier:@"MineSingleCollectionCellDef"];
    
    [self.mainView registerClass:[MineTwoPartCollectionCell class] forCellWithReuseIdentifier:@"MineTwoPartCollectionCellDef"];
    
    [self.mainView registerClass:[MineThreePartCollectionCell class] forCellWithReuseIdentifier:@"MineThreePartCollectionCellDef"];
}

#pragma mark - UICollectionViewDelegate And DataSource

- NSIntegernumberOfSectionsInCollectionView:UICollectionView *collectionView{
    return 4;
}

- NSIntegercollectionView:UICollectionView *collectionView numberOfItemsInSection:NSIntegersection{
    switch section {
        case 0:
            return 3;
        case 1:
            return 6;
        case 2:
        case 3:
            return 1;
        default:
            return 0;
            break;
    }
}

- UICollectionViewCell *collectionView:UICollectionView *collectionView cellForItemAtIndexPath:NSIndexPath *indexPath{
    UICollectionViewCell *cell = nil;
    switch indexPath.section {
        case 0:
        {
            switch indexPath.row {
                case 0:
                {
                    cell = self.msgCollectionCell; //xib拉出来的全局cell
                }
                    break;
                
                default:
                {
                    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineTwoPartCollectionCellDef" forIndexPath:indexPath];
                    
                }
                    break;
            }
        }
            break;
        case 1:
        {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineThreePartCollectionCellDef" forIndexPath:indexPath];
            
        }
            break;
        default:
        {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineSinglePartCollectionCellDef" forIndexPath:indexPath];
        }
            break;
    }
    
    return cell;
}

MineCollectionCustomLayout.h

#import <UIKit/UIKit.h>

@interface MineCollectionCustomLayout : UICollectionViewFlowLayout

@enda 

MineCollectionCustomLayout.m

#import "MineCollectionCustomLayout.h"

@implementation MineCollectionCustomLayout

-idinit
{
    self = [super init];
    if self {
        self.scrollDirection = UICollectionViewScrollDirectionVertical;

    }
    return self;
}

- voidprepareLayout{
    [super prepareLayout];
    
}


- CGSizecollectionViewContentSize{
    return CGSizeMakeSCREEN_WIDTH, SCREEN_HEIGHT;
}


-BOOLshouldInvalidateLayoutForBoundsChange:CGRectnewBounds

{
    
    return YES;
    
}

- UICollectionViewLayoutAttributes *layoutAttributesForItemAtIndexPath:NSIndexPath *indexPath{
    UICollectionViewLayoutAttributes *newAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    switch indexPath.section {
        case 0:
        {
            switch indexPath.row {
                case 0:
                {
                    newAttributes.size = CGSizeMakeSCREEN_WIDTH, 150;
                }
                    break;
                case 1:
                case 2:
                {
                    newAttributes.size = CGSizeMakeSCREEN_WIDTH/2.0, 80;
                }
                default:
                    break;
            }
        
        }
            
            break;
        case 1:
            newAttributes.size = CGSizeMakeSCREEN_WIDTH/3.0, 105;
            break;
        case 2:
        case 3:
            newAttributes.size = CGSizeMakeSCREEN_WIDTH, 50;
            break;
        default:
            break;
    }
    
    return newAttributes;
}

- NSArray<UICollectionViewLayoutAttributes *> *layoutAttributesForElementsInRect:CGRectrect{
    
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    
    NSMutableArray *newArray = [NSMutableArray new];
    for UICollectionViewLayoutAttributes *attributes in array {
        UICollectionViewLayoutAttributes *newAttributes = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
        [newArray addObject:newAttributes];
    }
    
    return newArray;
}

@end

最终效果是这样的 不知道代码哪里存在问题 求大家指导一下

(看完/读完)这篇文章有何感想! 999yzc亚洲城在线娱乐城的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注