#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;@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;@end@implementation WOCOHomeController- (void)viewDidLoad { [super viewDidLoad]; 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; 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; self.collectionView.allowsSelection = NO; self.collectionView.allowsMultipleSelection = YES; 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;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 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]; return cell;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { 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;}- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"取消上一次选中的第%zdcell", indexPath.item);}- (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
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(0, 2, 0, 2);}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 2;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 5;}
- 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