这篇为ViewController的制作

来源:互联网 发布:三菱plc编程实例108例 编辑:程序博客网 时间:2024/06/05 07:26

//

//  ViewController3.h

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/25.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController3 :UIViewController

{

  NSString * identifier;

  UIScrollView *scroll;

    

}


@end





//

//  ViewController3.m

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/25.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import "ViewController3.h"



@interface ViewController3 ()<UICollectionViewDataSource,UICollectionViewDelegate>


@end


UIImageView *imageView;

UIButton *button;


@implementation ViewController3


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorredColor];

    self.navigationItem.title =@"首页3";

    

    // 初始化 滑动视图

    scroll = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0,318,200)];

    scroll.pagingEnabled=YES;

    

    

    identifier = @"cell";

    

    // 初始化 flowLayout

    

    UICollectionViewFlowLayout * flowLayout =[[UICollectionViewFlowLayoutalloc] init];

    

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    

     // UIEdgeInsets top = {15,10,15,5};

    

     // [flowLayout setSectionInset:top];

    

    

    //创建头部 流动布局的尺寸

    flowLayout.headerReferenceSize =CGSizeMake(0,200);

    

    //创建CollectionView

    

   UICollectionView * collectionView =[[UICollectionViewalloc] initWithFrame:CGRectMake(0,0, 320, 568)collectionViewLayout:flowLayout];

    collectionView.backgroundColor = [UIColorlightGrayColor];

    

    

    //注册单元格

    

    [collectionViewregisterClass:[UICollectionViewCellclass]

       forCellWithReuseIdentifier:identifier];

    [collectionView registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"AAA头部"];

    

    //设置代理

    

    collectionView.delegate =self;

    

    collectionView.dataSource =self;

    

    [self.viewaddSubview:collectionView];

    

    

    

    

    

}


#pragma mark - collectionView delegate


//设置组数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView


{

    

   return 1;

    

}


//设置每个组有多少个cell

- (NSInteger)collectionView:(UICollectionView *)collectionView

     numberOfItemsInSection:(NSInteger)section


{

    

   return 20;

    

}


//定义每个UICollectionView 的间距(返回UIEdgeInsets:上、左、下、右)

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView

                       layout:(UICollectionViewLayout *)collectionViewLayout

       insetForSectionAtIndex:(NSInteger)section


{

    

   UIEdgeInsets top = {0,15,50,15};

    

   return top;

    

}


//设置每个cell大小(返回CGSize:宽度和高度)


-(CGSize)collectionView:(UICollectionView *)collectionView

                 layout:(UICollectionViewLayout *)collectionViewLayout

 sizeForItemAtIndexPath:(NSIndexPath *)indexPath


{

    

    returnCGSizeMake(140,200);

    

}



//定义每个UICollectionView 纵向的间距

- (CGFloat)collectionView:(UICollectionView *)collectionView

                   layout:(UICollectionViewLayout*)collectionViewLayout

minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

    

    

   return 0;

    

}



//头部显示的内容


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView

           viewForSupplementaryElementOfKind:(NSString *)kind

                                 atIndexPath:(NSIndexPath *)indexPath

{

    

    UICollectionReusableView *headerView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader

                                                                             withReuseIdentifier:@"AAA头部"

                                                                                    forIndexPath:indexPath];

    //headerView.backgroundColor = [UIColor whiteColor];

    

    //添加scorller

    

    [headerViewaddSubview:scroll];

    

    

    

   for (int i =0; i < 4; i++)

    {

       //图片

        

       imageView = [[UIImageViewalloc] initWithFrame:CGRectMake((i*318)+10,10, 300, 180)];

        

       imageView.backgroundColor = [UIColorblueColor];

        

       scroll.contentSize =CGSizeMake((i+1)*318,200);

        

        [scroll addSubview:imageView];

        

    }

    

    

    

   return headerView;

}



//显示每个cell内容


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView

                 cellForItemAtIndexPath:(NSIndexPath *)indexPath


{

    

    UICollectionViewCell * cell =[collectionViewdequeueReusableCellWithReuseIdentifier:identifier

                                                                          forIndexPath:indexPath];

    

    [cellsizeToFit];

    

    cell.backgroundColor =[UIColorpurpleColor];

    

    

   //按钮

    

    button = [[UIButtonalloc] initWithFrame:CGRectMake(10,10, 120, 180)];

    

    button.backgroundColor = [UIColororangeColor];

    

    //[button setTitle:@"按钮"  forState:UIControlStateNormal];

    

     [celladdSubview: button];

    

   //图片

    

    imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(10,10, 120, 130)];

    

    imageView.backgroundColor = [UIColorwhiteColor];

    

    

//    UIImage *image = [UIImage imageNamed:@"u=1070902365,2619384777&fm=21&gp=0.jpg"];

//    

//    [imageView addSubview:image];

    

    [celladdSubview:imageView];

    

    

   return cell;

    

}





@end


0 0