// // ViewController.m // 采集视图UICollectionView // // Created by dc0061 on 15/12/14. // Copyright © 2015年 dc0061. All rights reserved. // #import "ViewController.h" @interface ViewController () { UICollectionView *_collectionView; NSString *cellIdentifier;//cell重用标示符 } @end @implementation ViewController static int a=0; - (void)viewDidLoad { [super viewDidLoad]; cellIdentifier=@"reuseCell";//定义cell 的重用标示符 //布局ui [self layout]; } - (void) layout{ UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init]; //流式布局,能够设置cell的滚动方向水平滚动,默认的是垂直滚动 // flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal; _collectionView=[[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout]; _collectionView.backgroundColor=[UIColor grayColor]; _collectionView.delegate=self; _collectionView.dataSource=self; //注册采集视图所使用的cell,并设置标示符,cell的初始化由系统自动完成 //若是要自定义,只要将[UICollectionViewCell class]替换成自定义的cell类 [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier]; [self.view addSubview:_collectionView]; } //设置单元格cell数量 - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 166; } //设置cell - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; float num=(float)indexPath.row/66; cell.backgroundColor = [UIColor colorWithRed:0.9*num green:0.7*num blue:0.6*num alpha:1]; //设置cell选中时的颜色 UIView *selectView=[[UIView alloc]initWithFrame:cell.bounds]; selectView.backgroundColor=[UIColor brownColor]; cell.selectedBackgroundView=selectView; return cell; } //点击事件 - (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"7"); //获取cell UICollectionViewCell *cell=[collectionView cellForItemAtIndexPath:indexPath]; if(a==0){ cell.backgroundColor=[UIColor redColor]; a=1; }else{ cell.backgroundColor=[UIColor blueColor]; a=0; } } //设置cell的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{ return CGSizeMake(25, 25); } //设置顶部抬头距离 - (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(0, 20); } //设置内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(20, 20, 0, 20); } //设置cell行间距 - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 10; } //设置cell列间距 - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 10; } //交互 //事件的处理顺序,1.手指按下某个cell。2. //2.shouldHighlightItemAtIndexPath(若是返回yes,则向下执行,不然终止) //3. didHighlightItemAtIndexPath(高亮) //4.手指松开 //5.didUnhighlightItemAtIndexPath(取消高亮) //6. shouldSelectItemAtIndexPath (若是返回yes,则向下执行,不然终止) //7.didSelectItemAtIndexPath(执行选中事件) - (BOOL) collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"2"); return YES; } - (void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"3"); } - (void) collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"5"); } - (BOOL) collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"6"); return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end