UICollectionView

2020-04-24  本文已影响0人  liang1030
#import "AGHeaderPosterScrollController.h"
#import "AGHeaderPosterCollectionCell.h"

NSString * const AGHeaderPosterCollectionCellIdentifier = @"_AGHeaderPosterCollectionCellIdentifier";

@interface AGHeaderPosterScrollController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
/** 用来显示的假数据 */
@property (strong, nonatomic) NSMutableArray *dataArr;

@end

@implementation AGHeaderPosterScrollController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initUI];
}

- (void)initUI {
    [self.view addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_offset(16.0);
        make.right.equalTo(self.view).mas_offset(-16.0);
        make.top.bottom.equalTo(self.view);
    }];
}

#pragma mark ---UICollectionViewDelegateFlowLayout---

// 该方法是设置cell的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat itemWidth = (_ScreenWidth_-16.0*2-13.0)/2;
    CGFloat itemHeight = itemWidth/165.0*230.0;
    return CGSizeMake(itemWidth, itemHeight);
}

// 两个cell之间的最小间距,是由API自动计算的,只有当间距小于该值时,cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 13.0;
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 14.0;
}

#pragma mark ---UICollectionViewDelegate---

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark ---UICollectionViewDataSource---

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AGHeaderPosterCollectionCell *cell = (AGHeaderPosterCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:AGHeaderPosterCollectionCellIdentifier forIndexPath:indexPath];

    return cell;
}

//设置footer view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooterView" forIndexPath:indexPath];
        
        return footerview;
    }
    return nil;
}

//设置footer view尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(_ScreenWidth_, 34.0);
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(15.5, 0.0, 0.0, 0.0);//为区上左下右添加间距
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];// CollectionView的滚动方向
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[AGHeaderPosterCollectionCell class] forCellWithReuseIdentifier:AGHeaderPosterCollectionCellIdentifier];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooterView"];
        _collectionView.backgroundColor = UIColor.clearColor;
    }
    return _collectionView;
}

@end

上一篇 下一篇

猜你喜欢

热点阅读