UI10-UICollectionView

ViewController.m

#import "ViewController.h"
#import "UIColor+RandomColor.h"
#import "CustomCollectionViewCell.h"
#import "PhotoViewController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

//数据源
@property (nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //实例化数据源
    _dataSource = [NSMutableArray array];
    //循环添加RGB颜色到数据源
    for (NSInteger index = 0; index < 50; index ++) {
        [_dataSource addObject:[UIColor specialRandomColor]];
    }
    
    /**< CollectionView */
    //1.CollectionView布局(流式布局)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置 item 的尺寸
    layout.itemSize = CGSizeMake(100, 100);
    //设置 item 之间的最小间距
    layout.minimumInteritemSpacing = 4;
    //设置 item 之间的最小行距
    layout.minimumLineSpacing = 10;
    //header(宽和CollectionView是一样的,在此设置无意义)
    layout.headerReferenceSize = CGSizeMake(320, 60);
    //footer(宽和CollectionView是一样的,在此设置无意义)
    layout.footerReferenceSize = CGSizeMake(0, 60);
    
    //2.利用布局对象实例化 CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //背景颜色
    collectionView.backgroundColor = [UIColor whiteColor];
    //内偏移量(上,左,下,右)
    collectionView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
    //代理(处理响应)
    collectionView.delegate = self;
    //代理(展示数据)
    collectionView.dataSource = self;
    //注册cell
    [collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册header
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //注册 footer
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    //添加到视图
    [self.view addSubview:collectionView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"照片" style:UIBarButtonItemStylePlain target:self action:@selector(nextAction)];
}

- (void)nextAction{
    PhotoViewController *phVC = [[PhotoViewController alloc]init];
    [self.navigationController pushViewController:phVC animated:YES];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
//指定有几个 cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

//配置返回 cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //初始化
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //赋值
    cell.backgroundColor = _dataSource[indexPath.row];
    
    return cell;
}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        //header
        //取到header
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //设置背景颜色
        header.backgroundColor = [UIColor cyanColor];
        return header;
        
    }else{
        //footer
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        //设置背景颜色
        footer.backgroundColor = [UIColor cyanColor];
        return footer;
    }
    
}

//选择 cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    
   /**< 删除 */
//    //删除数据源
//    [_dataSource removeObjectAtIndex:indexPath.row];
//    //删除视图
//    [collectionView deleteItemsAtIndexPaths:@[indexPath]];
    
    /**< 插入 */
    //插入数据源
//    [_dataSource insertObject:[UIColor specialRandomColor] atIndex:indexPath.row];
//    //插入视图
//    [collectionView insertItemsAtIndexPaths:@[indexPath]];
    
    /**< 移动 */
    //移动数据源
    [_dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
    //移动视图
    [collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
}

@end
PhotoViewController.m

#import "PhotoViewController.h"

@interface PhotoViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
//图片视图
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    /**< 图片 */
    //初始化
    _imageView = [[UIImageView alloc] init];
    //大小
    _imageView.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds)*0.8, CGRectGetWidth(self.view.bounds)*0.8);
    //中心店位置
    _imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) - 100);
    //背景颜色
    _imageView.backgroundColor = [UIColor lightGrayColor];
    //添加到视图
    [self.view addSubview:_imageView];
    
    /**< 选择按钮 */
    //初始化
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //位置和大小
    button.frame = CGRectMake(150, 500, 80, 30);
    //按钮文字
    [button setTitle:@"选择" forState:UIControlStateNormal];
    //添加动作
    [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
    //添加到视图
    [self.view addSubview:button];
}

//button实现方法
- (void)buttonTaped:(UIButton *)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择照片" message:@"请你选择头像" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"选择照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self choosePhoto];
    }];
    
    UIAlertAction *takeAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takePhoto];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:photoAction];
    [alertController addAction:takeAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

//相册
- (void)choosePhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    //资源类型
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //代理
    picker.delegate = self;
    //允许编辑
    picker.allowsEditing = YES;
    
    //推送界面
    [self presentViewController:picker animated:YES completion:nil];
}

//拍照(不能拍照,因为模拟器不支持拍照)
- (void)takePhoto {
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        NSLog(@"你的手机不支持拍照,请更换设备");
        return;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    //资源类型
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //代理
    picker.delegate = self;
    //允许编辑
    picker.allowsEditing = YES;
    
    //推送界面
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@",info);
    //给image赋值
    _imageView.image = info[UIImagePickerControllerEditedImage];
    //返回
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
CustomCollectionViewCell.m

#import "CustomCollectionViewCell.h"

@implementation CustomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 20)];
        label.backgroundColor = [UIColor whiteColor];
        [self addSubview:label];
    }
    return self;
}

@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值