#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "MyCollectionViewCell2.h"
#define KCell @"mycell"
#define KHeader @"header"
#define KFooter @"footer"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> //创建collectionView遵守的两个协议,类似UITableView.
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 10;
flowLayout.minimumInteritemSpacing = 10;
CGFloat width = (self.view.bounds.size.width - 10 * 2 - 10 * 2) / 3;
CGFloat height = 2 * width;
flowLayout.itemSize = CGSizeMake(width, height);
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.headerReferenceSize = CGSizeMake(0, 100);
flowLayout.footerReferenceSize = CGSizeMake(0, 50);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor redColor];
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:KCell];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeader];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:KFooter];
[self.view addSubview:collectionView];
}
#pragma mark---------- 实现协议里面的方法 -------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCell forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"image2.jpg"];
cell.label.text = [NSString stringWithFormat:@"Sec:%ld Item:%ld", indexPath.section, indexPath.item];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KHeader forIndexPath:indexPath];
headerView.backgroundColor = [UIColor yellowColor];
return headerView;
} else {
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:KFooter forIndexPath:indexPath];
footerView.backgroundColor = [UIColor greenColor];
return footerView;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"分区号:%ld 元素下表:%ld", indexPath.section, indexPath.item);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface MyCollectionViewCell : UICollectionViewCell
@property (nonatomic, strong, readonly) UIImageView *imageView;
@property (nonatomic, strong, readonly) UILabel *label;
@end
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self.contentView addSubview:_imageView];
_label = [[UILabel alloc] initWithFrame:(CGRectMake(0, self.bounds.origin.y , self.bounds.size.width, 40))];
[self.contentView addSubview:_label];
}
return self;
}
@end
#import <UIKit/UIKit.h>
@interface MyCollectionViewCell2 : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
#import "MyCollectionViewCell2.h"
@implementation MyCollectionViewCell2
- (void)awakeFromNib {
}
@end