// // 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