├── AdvertisingColumn.h ├── AdvertisingColumn.m ├── CollectionViewCell.h ├── CollectionViewCell.m ├── IMAGE └── demo.png ├── README.md ├── cat.png ├── 代码创建UICollectionView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── seejoys.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── seejoys.xcuserdatad │ └── xcschemes │ ├── xcschememanagement.plist │ └── 代码创建UICollectionView.xcscheme └── 代码创建UICollectionView ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m /AdvertisingColumn.h: -------------------------------------------------------------------------------- 1 | // 2 | // AdvertisingColumn.h 3 | // CustomTabBar 4 | // 5 | // Created by shikee_app05 on 14-12-30. 6 | // Copyright (c) 2014年 chan kaching. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AdvertisingColumn : UIView{ 12 | 13 | NSTimer *_timer; 14 | } 15 | 16 | //广告栏 17 | @property (nonatomic,strong) UIScrollView *scrollView; 18 | @property (nonatomic,strong) UIPageControl *pageControl; 19 | @property (nonatomic,strong) UILabel *imageNum; 20 | @property (nonatomic) NSInteger totalNum; 21 | 22 | - (void)setArray:(NSArray *)imgArray; 23 | 24 | - (void)openTimer; 25 | - (void)closeTimer; 26 | 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /AdvertisingColumn.m: -------------------------------------------------------------------------------- 1 | // 2 | // AdvertisingColumn.m 3 | // CustomTabBar 4 | // 5 | // Created by shikee_app05 on 14-12-30. 6 | // Copyright (c) 2014年 chan kaching. All rights reserved. 7 | // 8 | 9 | #import "AdvertisingColumn.h" 10 | 11 | @implementation AdvertisingColumn 12 | 13 | - (id)initWithFrame:(CGRect)frame 14 | { 15 | self = [super initWithFrame:frame]; 16 | if (self) { 17 | // Initialization code 18 | _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))]; 19 | _scrollView.backgroundColor = [UIColor purpleColor]; 20 | _scrollView.delegate = self;//设置代理UIscrollViewDelegate 21 | _scrollView.showsVerticalScrollIndicator = NO;//是否显示竖向滚动条 22 | _scrollView.showsHorizontalScrollIndicator = NO;//是否显示横向滚动条 23 | _scrollView.pagingEnabled = YES;//是否设置分页 24 | 25 | [self addSubview:_scrollView]; 26 | 27 | /* 28 | ***容器,装载 29 | */ 30 | UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame), 20)]; 31 | containerView.backgroundColor = [UIColor clearColor]; 32 | [self addSubview:containerView]; 33 | UIView *alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame))]; 34 | alphaView.backgroundColor = [UIColor orangeColor]; 35 | alphaView.alpha = 0.7; 36 | [containerView addSubview:alphaView]; 37 | 38 | //分页控制 39 | _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)]; 40 | _pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//貌似不起作用呢 41 | _pageControl.currentPage = 0; //初始页码为0 42 | // _pageControl.backgroundColor = [UIColor greenColor]; 43 | [containerView addSubview:_pageControl]; 44 | //图片张数 45 | _imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)]; 46 | _imageNum.font = [UIFont boldSystemFontOfSize:15]; 47 | _imageNum.backgroundColor = [UIColor clearColor]; 48 | _imageNum.textColor = [UIColor whiteColor]; 49 | _imageNum.textAlignment = NSTextAlignmentRight; 50 | [containerView addSubview:_imageNum]; 51 | /* 52 | ***配置定时器,自动滚动广告栏 53 | */ 54 | _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; 55 | [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode]; 56 | [_timer setFireDate:[NSDate distantFuture]];//关闭定时器 57 | } 58 | return self; 59 | } 60 | //------------------------------------------------------------------------------------------- 61 | 62 | -(void)timerAction:(NSTimer *)timer{ 63 | if (_totalNum>1) { 64 | CGPoint newOffset = _scrollView.contentOffset; 65 | newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame); 66 | // NSLog(@"newOffset.x = %f",newOffset.x); 67 | if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) { 68 | newOffset.x = 0 ; 69 | } 70 | int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图 71 | newOffset.x = index * CGRectGetWidth(_scrollView.frame); 72 | _imageNum.text = [NSString stringWithFormat:@"%d / %ld",index+1,_totalNum]; 73 | [_scrollView setContentOffset:newOffset animated:YES]; 74 | }else{ 75 | [_timer setFireDate:[NSDate distantFuture]];//关闭定时器 76 | } 77 | } 78 | 79 | #pragma mark- PageControl绑定ScrollView 80 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView{//滚动就执行(会很多次) 81 | if ([scrollView isMemberOfClass:[UITableView class]]) { 82 | 83 | }else { 84 | int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图 85 | _pageControl.currentPage = index; 86 | for (UIView *view in scrollView.subviews) { 87 | if(view.tag == index){ 88 | 89 | }else{ 90 | 91 | } 92 | } 93 | } 94 | // NSLog(@"string%f",scrollView.contentOffset.x); 95 | } 96 | - (void)setArray:(NSArray *)imgArray{ 97 | _totalNum = [imgArray count]; 98 | if (_totalNum>0) { 99 | for (int i = 0; i<_totalNum; i++) { 100 | UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))]; 101 | img.contentMode = UIViewContentModeScaleAspectFill; 102 | img.image = [UIImage imageNamed:imgArray[i]]; 103 | //img.backgroundColor = imgArray[i]; 104 | [img setTag:i]; 105 | [_scrollView addSubview:img]; 106 | } 107 | _imageNum.text = [NSString stringWithFormat:@"%ld / %ld",_pageControl.currentPage+1,_totalNum]; 108 | _pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页 109 | CGRect frame; 110 | frame = _pageControl.frame; 111 | frame.size.width = 15*_totalNum; 112 | _pageControl.frame = frame; 113 | }else{ 114 | UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))]; 115 | [img setImage:[UIImage imageNamed:@"comment_gray"]]; 116 | img.userInteractionEnabled = YES; 117 | [_scrollView addSubview:img]; 118 | _imageNum.text = @"提示:滚动栏无数据。"; 119 | } 120 | _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页 121 | } 122 | - (void)openTimer{ 123 | [_timer setFireDate:[NSDate distantPast]];//开启定时器 124 | } 125 | - (void)closeTimer{ 126 | [_timer setFireDate:[NSDate distantFuture]];//关闭定时器 127 | } 128 | 129 | 130 | /* 131 | // Only override drawRect: if you perform custom drawing. 132 | // An empty implementation adversely affects performance during animation. 133 | - (void)drawRect:(CGRect)rect 134 | { 135 | // Drawing code 136 | } 137 | */ 138 | 139 | @end 140 | -------------------------------------------------------------------------------- /CollectionViewCell.h: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewCell.h 3 | // collectionView 4 | // 5 | // Created by shikee_app05 on 14-12-10. 6 | // Copyright (c) 2014年 shikee_app05. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CollectionViewCell : UICollectionViewCell 12 | 13 | @property(nonatomic ,strong)UIImageView *imgView; 14 | @property(nonatomic ,strong)UILabel *text; 15 | @property(nonatomic ,strong)UIButton *btn; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /CollectionViewCell.m: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewCell.m 3 | // collectionView 4 | // 5 | // Created by shikee_app05 on 14-12-10. 6 | // Copyright (c) 2014年 shikee_app05. All rights reserved. 7 | // 8 | 9 | #import "CollectionViewCell.h" 10 | 11 | @implementation CollectionViewCell 12 | 13 | - (id)initWithFrame:(CGRect)frame 14 | { 15 | self = [super initWithFrame:frame]; 16 | if (self) { 17 | // Initialization code 18 | self.backgroundColor = [UIColor purpleColor]; 19 | 20 | self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.frame)-10, CGRectGetWidth(self.frame)-10)]; 21 | self.imgView.backgroundColor = [UIColor groupTableViewBackgroundColor]; 22 | [self addSubview:self.imgView]; 23 | 24 | self.text = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(self.imgView.frame), CGRectGetWidth(self.frame)-10, 20)]; 25 | self.text.backgroundColor = [UIColor brownColor]; 26 | self.text.textAlignment = NSTextAlignmentCenter; 27 | [self addSubview:self.text]; 28 | 29 | self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; 30 | self.btn.frame = CGRectMake(5, CGRectGetMaxY(self.text.frame), CGRectGetWidth(self.frame)-10,30); 31 | [self.btn setTitle:@"按钮" forState:UIControlStateNormal]; 32 | self.btn.backgroundColor = [UIColor orangeColor]; 33 | [self addSubview:self.btn]; 34 | } 35 | return self; 36 | } 37 | 38 | /* 39 | // Only override drawRect: if you perform custom drawing. 40 | // An empty implementation adversely affects performance during animation. 41 | - (void)drawRect:(CGRect)rect 42 | { 43 | // Drawing code 44 | } 45 | */ 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /IMAGE/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CarrySniper/UICollectionView-Pure-code/464fbee24ecd75fbd05b4e14822402a3f4a2f94d/IMAGE/demo.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UICollectionView-Pure-code 2 | 使用纯代码创建UICollectionView,自定义cell,添加类似tableViewHeader的头部。主要代码在ViewController.m文件。 3 | 4 | ![](https://github.com/cjq002/UICollectionView-Pure-code/raw/master/IMAGE/demo.png) 5 | 6 | #pragma mark - 创建collectionView并设置代理 7 | ``` 8 | - (UICollectionView *)collectionView{ 9 | if (_collectionView == nil) { 10 | 11 | UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 12 | 13 | flowLayout.headerReferenceSize = CGSizeMake(fDeviceWidth, AD_height+10);//头部大小 14 | 15 | _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight) collectionViewLayout:flowLayout]; 16 | 17 | //定义每个UICollectionView 的大小 18 | flowLayout.itemSize = CGSizeMake((fDeviceWidth-20)/2, (fDeviceWidth-20)/2+50); 19 | //定义每个UICollectionView 横向的间距 20 | flowLayout.minimumLineSpacing = 5; 21 | //定义每个UICollectionView 纵向的间距 22 | flowLayout.minimumInteritemSpacing = 0; 23 | //定义每个UICollectionView 的边距距 24 | flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 5, 5);//上左下右 25 | 26 | //注册cell和ReusableView(相当于头部) 27 | [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 28 | [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"]; 29 | 30 | //设置代理 31 | _collectionView.delegate = self; 32 | _collectionView.dataSource = self; 33 | 34 | //背景颜色 35 | _collectionView.backgroundColor = [UIColor whiteColor]; 36 | //自适应大小 37 | _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 38 | 39 | } 40 | return _collectionView; 41 | } 42 | ``` 43 | 44 | #pragma mark - UICollectionView delegate dataSource 45 | ``` 46 | #pragma mark 定义展示的UICollectionViewCell的个数 47 | -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 48 | { 49 | return 30; 50 | } 51 | 52 | #pragma mark 定义展示的Section的个数 53 | -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 54 | { 55 | return 1; 56 | } 57 | 58 | #pragma mark 每个UICollectionView展示的内容 59 | -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 60 | { 61 | static NSString *identify = @"cell"; 62 | CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; 63 | [cell sizeToFit]; 64 | 65 | cell.imgView.image = [UIImage imageNamed:@"cat.png"]; 66 | cell.text.text = [NSString stringWithFormat:@"Cell %ld",indexPath.item]; 67 | 68 | return cell; 69 | } 70 | 71 | #pragma mark 头部显示的内容 72 | - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { 73 | 74 | UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: 75 | UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath]; 76 | 77 | [headerView addSubview:_headerView];//头部广告栏 78 | return headerView; 79 | } 80 | ``` 81 | 82 | #pragma mark UICollectionView被选中时调用的方法 83 | ``` 84 | -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 85 | { 86 | NSLog(@"选择%ld",indexPath.item); 87 | } 88 | ``` 89 | -------------------------------------------------------------------------------- /cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CarrySniper/UICollectionView-Pure-code/464fbee24ecd75fbd05b4e14822402a3f4a2f94d/cat.png -------------------------------------------------------------------------------- /代码创建UICollectionView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 52B833F01CEC4360001062A2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B833EF1CEC4360001062A2 /* main.m */; }; 11 | 52B833F31CEC4360001062A2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B833F21CEC4360001062A2 /* AppDelegate.m */; }; 12 | 52B833F61CEC4360001062A2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B833F51CEC4360001062A2 /* ViewController.m */; }; 13 | 52B833FB1CEC4360001062A2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 52B833FA1CEC4360001062A2 /* Assets.xcassets */; }; 14 | 52B833FE1CEC4360001062A2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 52B833FC1CEC4360001062A2 /* LaunchScreen.storyboard */; }; 15 | 52B8340A1CEC43BA001062A2 /* AdvertisingColumn.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B834061CEC43BA001062A2 /* AdvertisingColumn.m */; }; 16 | 52B8340B1CEC43BA001062A2 /* cat.png in Resources */ = {isa = PBXBuildFile; fileRef = 52B834071CEC43BA001062A2 /* cat.png */; }; 17 | 52B8340C1CEC43BA001062A2 /* CollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 52B834091CEC43BA001062A2 /* CollectionViewCell.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 52B833EB1CEC4360001062A2 /* 代码创建UICollectionView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "代码创建UICollectionView.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 52B833EF1CEC4360001062A2 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | 52B833F11CEC4360001062A2 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | 52B833F21CEC4360001062A2 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | 52B833F41CEC4360001062A2 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ViewController.h; path = "代码创建UICollectionView/ViewController.h"; sourceTree = ""; }; 26 | 52B833F51CEC4360001062A2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ViewController.m; path = "代码创建UICollectionView/ViewController.m"; sourceTree = ""; }; 27 | 52B833FA1CEC4360001062A2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 52B833FD1CEC4360001062A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 52B833FF1CEC4360001062A2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 52B834051CEC43BA001062A2 /* AdvertisingColumn.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AdvertisingColumn.h; sourceTree = ""; }; 31 | 52B834061CEC43BA001062A2 /* AdvertisingColumn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AdvertisingColumn.m; sourceTree = ""; }; 32 | 52B834071CEC43BA001062A2 /* cat.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cat.png; sourceTree = ""; }; 33 | 52B834081CEC43BA001062A2 /* CollectionViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CollectionViewCell.h; sourceTree = ""; }; 34 | 52B834091CEC43BA001062A2 /* CollectionViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CollectionViewCell.m; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | 52B833E81CEC4360001062A2 /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | 52B833E21CEC4360001062A2 = { 49 | isa = PBXGroup; 50 | children = ( 51 | 52B834051CEC43BA001062A2 /* AdvertisingColumn.h */, 52 | 52B834061CEC43BA001062A2 /* AdvertisingColumn.m */, 53 | 52B834071CEC43BA001062A2 /* cat.png */, 54 | 52B834081CEC43BA001062A2 /* CollectionViewCell.h */, 55 | 52B834091CEC43BA001062A2 /* CollectionViewCell.m */, 56 | 52B833F41CEC4360001062A2 /* ViewController.h */, 57 | 52B833F51CEC4360001062A2 /* ViewController.m */, 58 | 52B833ED1CEC4360001062A2 /* 代码创建UICollectionView */, 59 | 52B833EC1CEC4360001062A2 /* Products */, 60 | ); 61 | sourceTree = ""; 62 | }; 63 | 52B833EC1CEC4360001062A2 /* Products */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 52B833EB1CEC4360001062A2 /* 代码创建UICollectionView.app */, 67 | ); 68 | name = Products; 69 | sourceTree = ""; 70 | }; 71 | 52B833ED1CEC4360001062A2 /* 代码创建UICollectionView */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 52B833F11CEC4360001062A2 /* AppDelegate.h */, 75 | 52B833F21CEC4360001062A2 /* AppDelegate.m */, 76 | 52B833FA1CEC4360001062A2 /* Assets.xcassets */, 77 | 52B833FC1CEC4360001062A2 /* LaunchScreen.storyboard */, 78 | 52B833FF1CEC4360001062A2 /* Info.plist */, 79 | 52B833EE1CEC4360001062A2 /* Supporting Files */, 80 | ); 81 | path = "代码创建UICollectionView"; 82 | sourceTree = ""; 83 | }; 84 | 52B833EE1CEC4360001062A2 /* Supporting Files */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 52B833EF1CEC4360001062A2 /* main.m */, 88 | ); 89 | name = "Supporting Files"; 90 | sourceTree = ""; 91 | }; 92 | /* End PBXGroup section */ 93 | 94 | /* Begin PBXNativeTarget section */ 95 | 52B833EA1CEC4360001062A2 /* 代码创建UICollectionView */ = { 96 | isa = PBXNativeTarget; 97 | buildConfigurationList = 52B834021CEC4360001062A2 /* Build configuration list for PBXNativeTarget "代码创建UICollectionView" */; 98 | buildPhases = ( 99 | 52B833E71CEC4360001062A2 /* Sources */, 100 | 52B833E81CEC4360001062A2 /* Frameworks */, 101 | 52B833E91CEC4360001062A2 /* Resources */, 102 | ); 103 | buildRules = ( 104 | ); 105 | dependencies = ( 106 | ); 107 | name = "代码创建UICollectionView"; 108 | productName = "代码创建UICollectionView"; 109 | productReference = 52B833EB1CEC4360001062A2 /* 代码创建UICollectionView.app */; 110 | productType = "com.apple.product-type.application"; 111 | }; 112 | /* End PBXNativeTarget section */ 113 | 114 | /* Begin PBXProject section */ 115 | 52B833E31CEC4360001062A2 /* Project object */ = { 116 | isa = PBXProject; 117 | attributes = { 118 | LastUpgradeCheck = 0730; 119 | ORGANIZATIONNAME = Seejoys; 120 | TargetAttributes = { 121 | 52B833EA1CEC4360001062A2 = { 122 | CreatedOnToolsVersion = 7.3.1; 123 | }; 124 | }; 125 | }; 126 | buildConfigurationList = 52B833E61CEC4360001062A2 /* Build configuration list for PBXProject "代码创建UICollectionView" */; 127 | compatibilityVersion = "Xcode 3.2"; 128 | developmentRegion = English; 129 | hasScannedForEncodings = 0; 130 | knownRegions = ( 131 | en, 132 | Base, 133 | ); 134 | mainGroup = 52B833E21CEC4360001062A2; 135 | productRefGroup = 52B833EC1CEC4360001062A2 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 52B833EA1CEC4360001062A2 /* 代码创建UICollectionView */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | 52B833E91CEC4360001062A2 /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 52B833FE1CEC4360001062A2 /* LaunchScreen.storyboard in Resources */, 150 | 52B833FB1CEC4360001062A2 /* Assets.xcassets in Resources */, 151 | 52B8340B1CEC43BA001062A2 /* cat.png in Resources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXResourcesBuildPhase section */ 156 | 157 | /* Begin PBXSourcesBuildPhase section */ 158 | 52B833E71CEC4360001062A2 /* Sources */ = { 159 | isa = PBXSourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 52B8340A1CEC43BA001062A2 /* AdvertisingColumn.m in Sources */, 163 | 52B8340C1CEC43BA001062A2 /* CollectionViewCell.m in Sources */, 164 | 52B833F61CEC4360001062A2 /* ViewController.m in Sources */, 165 | 52B833F31CEC4360001062A2 /* AppDelegate.m in Sources */, 166 | 52B833F01CEC4360001062A2 /* main.m in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | 52B833FC1CEC4360001062A2 /* LaunchScreen.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | 52B833FD1CEC4360001062A2 /* Base */, 177 | ); 178 | name = LaunchScreen.storyboard; 179 | sourceTree = ""; 180 | }; 181 | /* End PBXVariantGroup section */ 182 | 183 | /* Begin XCBuildConfiguration section */ 184 | 52B834001CEC4360001062A2 /* Debug */ = { 185 | isa = XCBuildConfiguration; 186 | buildSettings = { 187 | ALWAYS_SEARCH_USER_PATHS = NO; 188 | CLANG_ANALYZER_NONNULL = YES; 189 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 190 | CLANG_CXX_LIBRARY = "libc++"; 191 | CLANG_ENABLE_MODULES = YES; 192 | CLANG_ENABLE_OBJC_ARC = YES; 193 | CLANG_WARN_BOOL_CONVERSION = YES; 194 | CLANG_WARN_CONSTANT_CONVERSION = YES; 195 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 196 | CLANG_WARN_EMPTY_BODY = YES; 197 | CLANG_WARN_ENUM_CONVERSION = YES; 198 | CLANG_WARN_INT_CONVERSION = YES; 199 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 200 | CLANG_WARN_UNREACHABLE_CODE = YES; 201 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 202 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 203 | COPY_PHASE_STRIP = NO; 204 | DEBUG_INFORMATION_FORMAT = dwarf; 205 | ENABLE_STRICT_OBJC_MSGSEND = YES; 206 | ENABLE_TESTABILITY = YES; 207 | GCC_C_LANGUAGE_STANDARD = gnu99; 208 | GCC_DYNAMIC_NO_PIC = NO; 209 | GCC_NO_COMMON_BLOCKS = YES; 210 | GCC_OPTIMIZATION_LEVEL = 0; 211 | GCC_PREPROCESSOR_DEFINITIONS = ( 212 | "DEBUG=1", 213 | "$(inherited)", 214 | ); 215 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 216 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 217 | GCC_WARN_UNDECLARED_SELECTOR = YES; 218 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 219 | GCC_WARN_UNUSED_FUNCTION = YES; 220 | GCC_WARN_UNUSED_VARIABLE = YES; 221 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 222 | MTL_ENABLE_DEBUG_INFO = YES; 223 | ONLY_ACTIVE_ARCH = YES; 224 | SDKROOT = iphoneos; 225 | }; 226 | name = Debug; 227 | }; 228 | 52B834011CEC4360001062A2 /* Release */ = { 229 | isa = XCBuildConfiguration; 230 | buildSettings = { 231 | ALWAYS_SEARCH_USER_PATHS = NO; 232 | CLANG_ANALYZER_NONNULL = YES; 233 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 234 | CLANG_CXX_LIBRARY = "libc++"; 235 | CLANG_ENABLE_MODULES = YES; 236 | CLANG_ENABLE_OBJC_ARC = YES; 237 | CLANG_WARN_BOOL_CONVERSION = YES; 238 | CLANG_WARN_CONSTANT_CONVERSION = YES; 239 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 240 | CLANG_WARN_EMPTY_BODY = YES; 241 | CLANG_WARN_ENUM_CONVERSION = YES; 242 | CLANG_WARN_INT_CONVERSION = YES; 243 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 244 | CLANG_WARN_UNREACHABLE_CODE = YES; 245 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 246 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 247 | COPY_PHASE_STRIP = NO; 248 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 249 | ENABLE_NS_ASSERTIONS = NO; 250 | ENABLE_STRICT_OBJC_MSGSEND = YES; 251 | GCC_C_LANGUAGE_STANDARD = gnu99; 252 | GCC_NO_COMMON_BLOCKS = YES; 253 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 254 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 255 | GCC_WARN_UNDECLARED_SELECTOR = YES; 256 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 257 | GCC_WARN_UNUSED_FUNCTION = YES; 258 | GCC_WARN_UNUSED_VARIABLE = YES; 259 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 260 | MTL_ENABLE_DEBUG_INFO = NO; 261 | SDKROOT = iphoneos; 262 | VALIDATE_PRODUCT = YES; 263 | }; 264 | name = Release; 265 | }; 266 | 52B834031CEC4360001062A2 /* Debug */ = { 267 | isa = XCBuildConfiguration; 268 | buildSettings = { 269 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 270 | INFOPLIST_FILE = "代码创建UICollectionView/Info.plist"; 271 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 272 | PRODUCT_BUNDLE_IDENTIFIER = "com.seejoys.----UICollectionView"; 273 | PRODUCT_NAME = "$(TARGET_NAME)"; 274 | }; 275 | name = Debug; 276 | }; 277 | 52B834041CEC4360001062A2 /* Release */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 281 | INFOPLIST_FILE = "代码创建UICollectionView/Info.plist"; 282 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 283 | PRODUCT_BUNDLE_IDENTIFIER = "com.seejoys.----UICollectionView"; 284 | PRODUCT_NAME = "$(TARGET_NAME)"; 285 | }; 286 | name = Release; 287 | }; 288 | /* End XCBuildConfiguration section */ 289 | 290 | /* Begin XCConfigurationList section */ 291 | 52B833E61CEC4360001062A2 /* Build configuration list for PBXProject "代码创建UICollectionView" */ = { 292 | isa = XCConfigurationList; 293 | buildConfigurations = ( 294 | 52B834001CEC4360001062A2 /* Debug */, 295 | 52B834011CEC4360001062A2 /* Release */, 296 | ); 297 | defaultConfigurationIsVisible = 0; 298 | defaultConfigurationName = Release; 299 | }; 300 | 52B834021CEC4360001062A2 /* Build configuration list for PBXNativeTarget "代码创建UICollectionView" */ = { 301 | isa = XCConfigurationList; 302 | buildConfigurations = ( 303 | 52B834031CEC4360001062A2 /* Debug */, 304 | 52B834041CEC4360001062A2 /* Release */, 305 | ); 306 | defaultConfigurationIsVisible = 0; 307 | defaultConfigurationName = Release; 308 | }; 309 | /* End XCConfigurationList section */ 310 | }; 311 | rootObject = 52B833E31CEC4360001062A2 /* Project object */; 312 | } 313 | -------------------------------------------------------------------------------- /代码创建UICollectionView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /代码创建UICollectionView.xcodeproj/project.xcworkspace/xcuserdata/seejoys.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CarrySniper/UICollectionView-Pure-code/464fbee24ecd75fbd05b4e14822402a3f4a2f94d/代码创建UICollectionView.xcodeproj/project.xcworkspace/xcuserdata/seejoys.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /代码创建UICollectionView.xcodeproj/xcuserdata/seejoys.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | 代码创建UICollectionView.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 52B833EA1CEC4360001062A2 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /代码创建UICollectionView.xcodeproj/xcuserdata/seejoys.xcuserdatad/xcschemes/代码创建UICollectionView.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /代码创建UICollectionView/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // 代码创建UICollectionView 4 | // 5 | // Created by 思久科技 on 16/5/18. 6 | // Copyright © 2016年 Seejoys. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /代码创建UICollectionView/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // 代码创建UICollectionView 4 | // 5 | // Created by 思久科技 on 16/5/18. 6 | // Copyright © 2016年 Seejoys. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "ViewController.h" 11 | 12 | @implementation AppDelegate 13 | 14 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 15 | { 16 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 17 | // Override point for customization after application launch. 18 | UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; 19 | self.window.rootViewController = navC; 20 | 21 | self.window.backgroundColor = [UIColor whiteColor]; 22 | [self.window makeKeyAndVisible]; 23 | return YES; 24 | } 25 | 26 | - (void)applicationWillResignActive:(UIApplication *)application { 27 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 28 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 29 | } 30 | 31 | - (void)applicationDidEnterBackground:(UIApplication *)application { 32 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 33 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 34 | } 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | - (void)applicationDidBecomeActive:(UIApplication *)application { 41 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 42 | } 43 | 44 | - (void)applicationWillTerminate:(UIApplication *)application { 45 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /代码创建UICollectionView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /代码创建UICollectionView/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /代码创建UICollectionView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /代码创建UICollectionView/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // 代码创建UICollectionView 4 | // 5 | // Created by 陈家庆 on 15-2-6. 6 | // Copyright (c) 2015年 shikee_Chan. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AdvertisingColumn.h"//头部滚动的,不需要可以去掉 11 | 12 | @interface ViewController : UIViewController{ 13 | AdvertisingColumn *_headerView; //广告栏 14 | NSMutableArray *_cellArray; //collectionView数据 15 | } 16 | 17 | @property (nonatomic, strong) UICollectionView *collectionView; 18 | 19 | @end 20 | 21 | -------------------------------------------------------------------------------- /代码创建UICollectionView/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // 代码创建UICollectionView 4 | // 5 | // Created by 陈家庆 on 15-2-6. 6 | // Copyright (c) 2015年 shikee_Chan. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "CollectionViewCell.h" 11 | 12 | #define fDeviceWidth ([UIScreen mainScreen].bounds.size.width) 13 | #define fDeviceHeight ([UIScreen mainScreen].bounds.size.height) 14 | static float AD_height = 150;//广告栏高度 15 | 16 | @interface ViewController () 17 | 18 | @end 19 | 20 | @implementation ViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | // Do any additional setup after loading the view. 25 | //导航栏背景颜色 26 | [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]]; 27 | [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:20.0f],NSFontAttributeName, nil]]; 28 | self.navigationItem.title = @"自定义collectionView"; 29 | 30 | /** 31 | * 创建collectionView self自动调用setter getter方法 32 | */ 33 | [self.view addSubview:self.collectionView]; 34 | 35 | /** 36 | * 广告栏 37 | */ 38 | _headerView = [[AdvertisingColumn alloc]initWithFrame:CGRectMake(5, 5, fDeviceWidth-10, AD_height)]; 39 | _headerView.backgroundColor = [UIColor blackColor]; 40 | 41 | /** 42 | * 加载的数据 43 | */ 44 | NSArray *imgArray = [NSArray arrayWithObjects:@"cat.png",@"cat.png",@"cat.png",@"cat.png",@"cat.png",@"cat.png", nil]; 45 | [_headerView setArray:imgArray]; 46 | 47 | //collectionView数据 48 | _cellArray = [imgArray mutableCopy]; 49 | } 50 | 51 | #pragma mark - 创建collectionView并设置代理 52 | - (UICollectionView *)collectionView 53 | { 54 | if (_collectionView == nil) { 55 | 56 | UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 57 | 58 | flowLayout.headerReferenceSize = CGSizeMake(fDeviceWidth, AD_height+10);//头部大小 59 | 60 | _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight) collectionViewLayout:flowLayout]; 61 | 62 | //定义每个UICollectionView 的大小 63 | flowLayout.itemSize = CGSizeMake((fDeviceWidth-20)/2, (fDeviceWidth-20)/2+50); 64 | //定义每个UICollectionView 横向的间距 65 | flowLayout.minimumLineSpacing = 5; 66 | //定义每个UICollectionView 纵向的间距 67 | flowLayout.minimumInteritemSpacing = 0; 68 | //定义每个UICollectionView 的边距距 69 | flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 5, 5);//上左下右 70 | 71 | //注册cell和ReusableView(相当于头部) 72 | [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 73 | [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"]; 74 | 75 | //设置代理 76 | _collectionView.delegate = self; 77 | _collectionView.dataSource = self; 78 | 79 | //背景颜色 80 | _collectionView.backgroundColor = [UIColor whiteColor]; 81 | //自适应大小 82 | _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 83 | 84 | } 85 | return _collectionView; 86 | } 87 | 88 | #pragma mark - 定时滚动scrollView 89 | -(void)viewDidAppear:(BOOL)animated {//显示窗口 90 | [super viewDidAppear:animated]; 91 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 92 | [_headerView openTimer];//开启定时器 93 | }); 94 | } 95 | -(void)viewWillDisappear:(BOOL)animated {//将要隐藏窗口 96 | [super viewWillDisappear:animated]; 97 | if (_headerView.totalNum>1) { 98 | [_headerView closeTimer];//关闭定时器 99 | } 100 | } 101 | #pragma mark - scrollView也是适用于tableView的cell滚动 将开始和将要结束滚动时调用 102 | - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 103 | [_headerView closeTimer];//关闭定时器 104 | } 105 | - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 106 | if (_headerView.totalNum>1) { 107 | [_headerView openTimer];//开启定时器 108 | } 109 | } 110 | 111 | #pragma mark - UICollectionView delegate dataSource 112 | #pragma mark 定义展示的UICollectionViewCell的个数 113 | -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 114 | { 115 | return [_cellArray count]; 116 | } 117 | 118 | #pragma mark 定义展示的Section的个数 119 | -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 120 | { 121 | return 1; 122 | } 123 | 124 | #pragma mark 每个UICollectionView展示的内容 125 | -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 126 | { 127 | static NSString *identify = @"cell"; 128 | CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; 129 | [cell sizeToFit]; 130 | 131 | cell.imgView.image = [UIImage imageNamed:_cellArray[indexPath.item]]; 132 | cell.text.text = [NSString stringWithFormat:@"Cell %ld",indexPath.item]; 133 | //按钮事件就不实现了…… 134 | return cell; 135 | } 136 | 137 | #pragma mark 头部显示的内容 138 | - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 139 | { 140 | UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: 141 | UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath]; 142 | 143 | [headerView addSubview:_headerView];//头部广告栏 144 | return headerView; 145 | } 146 | 147 | #pragma mark UICollectionView被选中时调用的方法 148 | -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 149 | { 150 | NSLog(@"选择%ld",indexPath.item); 151 | } 152 | 153 | - (void)didReceiveMemoryWarning { 154 | [super didReceiveMemoryWarning]; 155 | // Dispose of any resources that can be recreated. 156 | } 157 | 158 | /* 159 | #pragma mark - Navigation 160 | 161 | // In a storyboard-based application, you will often want to do a little preparation before navigation 162 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 163 | // Get the new view controller using [segue destinationViewController]. 164 | // Pass the selected object to the new view controller. 165 | } 166 | */ 167 | 168 | @end 169 | -------------------------------------------------------------------------------- /代码创建UICollectionView/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // 代码创建UICollectionView 4 | // 5 | // Created by 思久科技 on 16/5/18. 6 | // Copyright © 2016年 Seejoys. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------