//
// ViewController.m
// UI09_ControllerView
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
#import "ImageCell.h"
@interface ViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) NSMutableArray *imageArray;
@end
@implementation ViewController
- (void)getData {
self.imageArray = [NSMutableArray array];
for (int i = 1; i <= 7; i++) {
NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg",i];
UIImage *image = [UIImage imageNamed:imageName];
[self.imageArray addObject:image];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
[self getData];
//UICollectionViewLayout的子类,网格样式布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//每个item的尺寸(collectionView会根据item的尺寸自动调整行数和列数)
//7P屏幕宽度宽度 除得的结果为float型时,可能会出现四舍五入的情况,导致计算总和超出屏幕宽度,所以做一个取整舍弃小数部分的值
flowLayout.itemSize = CGSizeMake((int)(self.view.bounds.size.width-4*10)/3, 200);
// flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, 150);
//滑动方向(系统默认垂直方向)
// flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//调整最小的间距(垂直方向滑动设置的是行的最小间距,水平方向滑动设置的是列的最小间距)
// flowLayout.minimumLineSpacing = 0;
//在垂直方向滑动:设置的是同一行item之间的间距
//在水平方向滑动:设置的是同一列item之间的间距
flowLayout.minimumInteritemSpacing = 0;
flowLayout.sectionInset = UIEdgeInsetsMake(30, 10, 10, 10);
//参数1:collectionView的frame
//参数2:加载布局对象
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.delegate = self;
collectionView.dataSource = self;
//注册后直接重用
[collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:collectionView];
}
//
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor orangeColor];
cell.myImage = self.imageArray[indexPath.item];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// ImageCell.h
// UI09_ControllerView
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ImageCell : UICollectionViewCell
@property (nonatomic,retain) UIImage *myImage;
@end
//
// ImageCell.m
// UI09_ControllerView
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ImageCell.h"
@interface ImageCell ()
@property (nonatomic,retain) UIImageView *myImageView;
@end
@implementation ImageCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:self.myImageView];
}
return self;
}
- (void)setMyImage:(UIImage *)myImage {
if (_myImage != myImage) {
_myImage = myImage;
self.myImageView.image = myImage;
}
}
//布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
[super applyLayoutAttributes:layoutAttributes];
self.myImageView.frame = layoutAttributes.bounds;
}
@end
网友评论