ObObjective-c 採集試圖 UICollection

//spa

//  ViewController.m.net

//  12.14 採集試圖 UICollection3d

//orm

//  Created by DC017 on 15/12/14.事件

//  Copyright © 2015 DC017. All rights reserved.ci

//get


#import "ViewController.h"it


@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>io

{class

    UICollectionView * collection;

    //重用標示符

    NSString * CellIdenntifier;

    float f;

    

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    CellIdenntifier=@"標示";

    [self layout];

    f=0;

  

}

-(void)layout{

    UICollectionViewFlowLayout *bujufangan=[[UICollectionViewFlowLayout alloc]init];

     //設置滾動方向(垂直或水平)

          //翻頁

    //collection.pagingEnabled=YES;

    

    collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, 375, 647) collectionViewLayout:bujufangan];

    collection.backgroundColor=[UIColor orangeColor];

   

    //bujufangan.scrollDirection=//滾動方向

     //水平滾動

    bujufangan.scrollDirection=UICollectionViewScrollDirectionHorizontal;

    [self.view addSubview:collection];

    

    collection.delegate=self;

    collection.dataSource=self;

    //註冊採集試圖所使用的cell類,並設置標示符

    

    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdenntifier];

    

   

    

}

#pragma mark 設置Ceel

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    

    UICollectionViewCell *cell=[collection dequeueReusableCellWithReuseIdentifier:CellIdenntifier forIndexPath:indexPath];

   

    cell.backgroundColor=[UIColor colorWithWhite:(float)indexPath.row/(66*1.0) alpha:1];

    

    //選中後變紅色

//    UIView* view=[[UIView alloc]initWithFrame:cell.bounds];

//    view.backgroundColor=[UIColor redColor];

//    cell.selectedBackgroundView=view;


    

    return cell;

}

#pragma marc 設置單個section 裏的cell

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return  66;

}

#pragma mark 點擊事件

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

    



    

    NSLog(@"%ld",indexPath.row);

}

#pragma mark 自定義cell 尺寸

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    f=f+2;

    return CGSizeMake(f, f);

    

}

#pragma mark 定義每一個section 的內邊距

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    

    return UIEdgeInsetsMake(100, 40, 0, 40);

}

#pragma mark 設定cell行間距

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 600;

}

#pragma mark 設置列間距

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 20;

}

//交互

//1.手指按下某個cell

//2.shouldHighlightIemAtIndexPach(若是返回yes,則向下執行,不然終止);


//3.didHightlightItemAtIndexPath(高亮)

//4.手指鬆開

//5.didUnhighlghtItemAtIndexPath(取消高亮);

//6.shouldSelectItemAtIndexPath(若是返回yes,則繼續向下執行,不然終止)

//7.didSelectItemAtIndexPath(執行選擇事件)

-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    

    

    

}

-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    [collectionView cellForItemAtIndexPath:indexPath].backgroundColor = [UIColor whiteColor];

}

#pragma mark 頁眉  頁腳  水平width  垂直height

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeMake(200,10 );

   

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索