iOS 开发 UICollectionView纯代码设置

来源:互联网 发布:里仁洞淘宝村具体位置 编辑:程序博客网 时间:2024/05/22 05:02
////  WOCOHomeController.m//  demo////  Created by zhouyu on 2016/12/20.//  Copyright © 2016年 demo. All rights reserved.//#import "WOCOHomeController.h"#import "WOCOHomeHeadView.h"#import "NetworkTool.h"#import "WOCOHomeSelectTypeCell.h"#import "WOCOHomeDisplayCell.h"@interface WOCOHomeController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>/** *  头部视图 */@property (nonatomic, strong) WOCOHomeHeadView *homeHeadView;/** *  轮播图 */@property (nonatomic, copy) NSArray *imagesURLStringArr;/** *  自定义collectionView */@property (nonatomic, strong) UICollectionView *collectionView;/** *  自定义流布局flowLayout */@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;@end@implementation WOCOHomeController- (void)viewDidLoad {    [super viewDidLoad];    // MARK: - UICollectionViewFlowLayout流布局    // 通过控制器的此属性只能获取布局对象,不能更换布局对象    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];    flowLayout.itemSize = CGSizeMake(100, 100);    self.flowLayout = flowLayout;    // 设置滚动方向    // 当滚动方向发生颠倒后,最小行间距和列间距也会相应的颠倒,头部和尾部的宽或高也会发生相应颠倒    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;    // 设置最小行间距    self.flowLayout.minimumLineSpacing = 10;    // 设置最小列间距    self.flowLayout.minimumInteritemSpacing = 20;    // 设置头部视图的尺寸"垂直滚动时,头部的宽是改不了得,如果是水平方向滚动,头部的高是不能改得"    self.flowLayout.headerReferenceSize = CGSizeMake(20, 100);    // 设置尾部视图的尺寸"垂直滚动时,尾部的宽是改不了得,如果是水平方向滚动,尾部的高是不能改得"    self.flowLayout.footerReferenceSize = CGSizeMake(20, 100);    // 设置组的内边距    self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);    // 当前组还在可视范围内时头部是否停留    self.flowLayout.sectionHeadersPinToVisibleBounds = NO;    // 当前组还在可视范围内时尾部是否停留    self.flowLayout.sectionFootersPinToVisibleBounds = NO;    // MARK: - UICollectionView设置    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];    self.collectionView = collectionView;    [self.view addSubview:collectionView];    collectionView.backgroundColor = [UIColor whiteColor];    // 屏蔽指示器    collectionView.showsVerticalScrollIndicator = NO;    collectionView.showsHorizontalScrollIndicator = NO;    // 弹边效果    collectionView.bounces = YES;    // 分页效果    collectionView.pagingEnabled = YES;    // 选中cell    self.collectionView.allowsSelection = NO;    // 设置是否允许多选    self.collectionView.allowsMultipleSelection = YES;    // MARK: - UICollectionView代理数据源和注册cell,头尾部视图    collectionView.dataSource = self;    collectionView.delegate = self;    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];}#pragma mark - 数据源和代理方法// 返回有多少组- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return 5;}// 返回每一组有多少个cell- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return 20;}// 返回每一组的每一个cell- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    // 1.创建cell    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];    // 2.设置数据    cell.backgroundColor =  [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];    // 3.返回    return cell;}// 返回每一组的头部或尾部视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {    // 1.定义重用标识    static NSString *ID = @"header";    if (kind == UICollectionElementKindSectionFooter) {        ID = @"footer";    } else {        ID = @"header";    }    NSLog(@"%@", kind);    // 获取每一组的头部或尾部视图    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ID forIndexPath:indexPath];    reusableView.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];;    return reusableView;}// 代理方法// 取消上一次选中的某个cell- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {    NSLog(@"取消上一次选中的第%zdcell", indexPath.item);}// 选中某一个cell- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {    NSLog(@"选中第%zd个cell", indexPath.item);}// 流布局中的代理方法// 返回每一个格子的具体尺寸- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section % 2 == 0) {        return CGSizeMake(100, 100);    } else {        return CGSizeMake(80, 80);    }}#pragma mark - 懒加载- (NSArray *)imagesURLStringArr {    if (_imagesURLStringArr == nil) {        _imagesURLStringArr = @[                                @"http://www.wocoor.com/templates/images/banner01.jpg",                                @"http://www.wocoor.com/templates/images/banner02.jpg",                                @"http://www.wocoor.com/templates/images/banner03.jpg",                                @"http://www.wocoor.com/templates/images/banner04.jpg"                                ];    }    return _imagesURLStringArr;}@end
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
//footer的size//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section//{//    return CGSizeMake(10, 10);//}//header的size//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section//{//    return CGSizeMake(10, 10);//}//设置每个item的UIEdgeInsets- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{    return UIEdgeInsetsMake(0, 2, 0, 2);}//设置每个item水平间距- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{    return 2;}//设置每个item垂直间距- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{    return 5;}//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath//{//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];//    headerView.backgroundColor =[UIColor grayColor];//    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];//    label.text = @"这是collectionView的头部";//    label.font = [UIFont systemFontOfSize:20];//    [headerView addSubview:label];//    return headerView;//}
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

自定义headerview

#import <UIKit/UIKit.h>@interface myHeadView : UICollectionReusableView{    UILabel   *TitleLable;}-(void)setTextTitle;@end#import "myHeadView.h"@implementation myHeadView-(instancetype)initWithFrame:(CGRect)frame{    if (self =[super initWithFrame:frame]) {        [self createUI];    }    return self;}-(void)createUI{    TitleLable= [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];    [self addSubview:TitleLable];}//这是头部-(void)setTextTitle{    TitleLable.text=@"哈哈";}@end
  • 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
  • 29
  • 30
  • 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
  • 29
  • 30
//设置头尾部内容-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    UICollectionReusableView *reusableView =nil;    if (kind ==UICollectionElementKindSectionHeader) {        //定制头部视图的内容        MyHeaderView *headerV = (MyHeaderView *)[collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderView"forIndexPath:indexPath];        headerV.titleLab.text =@"头部视图";        reusableView = headerV;    }    if (kind ==UICollectionElementKindSectionFooter){        MyFooterView *footerV = (MyFooterView *)[collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"FooterView"forIndexPath:indexPath];        footerV.titleLab.text =@"尾部视图";        reusableView = footerV;    }   return reusableView;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
相关的代理调用- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    UICollectionReusableView *header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"forIndexPath:indexPath];    header.backgroundColor = RGB(236, 237,241);    if (indexPath.section ==0) {        labelOne.text =@"热门检查";        labelOne.font = [UIFontsystemFontOfSize:14.0f];        labelOne.textColor =MainRGB;        [header addSubview:labelOne];    }else{        labelTwo.text =@"疾病信息";        labelTwo.font = [UIFontsystemFontOfSize:14.0f];        labelTwo.textColor =MainRGB;        [header addSubview:labelTwo];    }    return header;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
0 0
原创粉丝点击