#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "FooterCollectionReusableView.h"
#import "NewViewController.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
? ? NSDictionary *_dic;
? }
//網(wǎng)格視圖的對象
@property(nonatomic,strong)UICollectionView *myCollection;
@end
@implementation ViewController
//重寫get方法
-(UICollectionView*)myCollection{
? ? if (!_myCollection) {
? ? ? ? //初始化布局對象
? ? ? ? UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
? ? ? ? //設(shè)置每個模塊的大小
? ? ? ? layout.itemSize=CGSizeMake(100, 100);
? ? ? ? //設(shè)置滾動方向
//? ? ? ? layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
? ? ? ? layout.scrollDirection = UICollectionViewScrollDirectionVertical;
? ? ? ? //設(shè)置最小行間距
? ? ? ? layout.minimumLineSpacing= 10;
? ? ? ? //設(shè)置最小列間距
? ? ? ? layout.minimumInteritemSpacing = 5;
? ? ? ? //設(shè)置頭視圖的大小 自動適配? (如果是垂直滾動的話 寬自動是屏幕的寬,可以設(shè)置高度展运。反之如果是水平滾動的話 高自動是屏幕的高崎苗,可以設(shè)置寬度。)
? ? ? ? layout.headerReferenceSize = CGSizeMake(100, 30);
? ? ? ? //設(shè)置尾視圖的大小
? ? ? ? layout.footerReferenceSize = CGSizeMake(100, 30);
? ? ? ? //設(shè)置每個模塊中 上 左 下 右的間距(模塊的邊界和模塊內(nèi)容的距離)
? ? ? ? layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
//? ? ? ? _myCollection? = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
? ? ? ? _myCollection? = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
? ? ? ? //設(shè)置代理
? ? ? ? _myCollection.delegate = self;
? ? ? ? _myCollection.dataSource = self;
? ? ? ? //注冊單元格
? ? ? ? [_myCollection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellId"];
? ? ? ? //注冊頭視圖單元格
? ? ? ? [_myCollection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
? ? ? ? //注冊尾視圖單元格
? ? ? ? [_myCollection registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
? ? ? ? //設(shè)置背景顏色
? ? ? ? _myCollection.backgroundColor = [UIColor lightGrayColor];
? ? }
? ? return _myCollection;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ? [self.view addSubview:self.myCollection];
? ? //模擬數(shù)據(jù)
?? // _dic =@{@"野獸":@[@"小濱",@"小李",@"小蔣",@"小澤"],
//? ? ? ? ? ? @"帥哥":@[@"小雨",@"小惠",@"小建",@"小金",@"小尹",@"小石頭"],
//? ? ? ? ? ? @"美女":@[@"小白",@"小玉",@"小霜",@"小小白"],
//? ? ? ? ? ? @"異類":@[@"小黑",@"陳碩",@"光頭"]
//? ? ? ? ? ? };
_dic = @{@"野獸":@[@{@"小濱":@"1.jpg"},@{@"小李":@"2.jpg"},@{@"小蔣":@"5.jpg"},@{@"小澤":@"3.jpg"}],
?? ? ? ? @"帥哥":@[@{@"小雨":@"1.jpg"},@{@"小惠":@"3.jpg"},@{@"小建":@"1.jpg"},@{@"小金":@"4.jpg"},@{@"小尹":@"6.jpg"},@{@"小石頭":@"1.jpg"}],
?? ? ? ? @"美女":@[@{@"小白":@"3.jpg"},@{@"小玉":@"7.jpg"},@{@"小霜":@"4.jpg"},@{@"小小白":@"2.jpg"}],
?? ? ? ? @"異類":@[@{@"小黑":@"5.jpg"},@{@"陳碩":@"7.jpg"},@{@"光頭":@"6.jpg"}]
?? ? ? ? ? ? };
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
? ? return _dic.count;
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section{
? ? NSString*str =_dic.allKeys[section];
? ? return [[_dic objectForKey:str] count];
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
? ? static NSString *cellId =@"collectionCellId";
? ? MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId? forIndexPath:indexPath];
? ? NSString *str = _dic.allKeys[indexPath.section];
? ? NSString *name = [[[_dic objectForKey:str]objectAtIndex:indexPath.row] allKeys][0];
? ? cell.imageView.image = [UIImage imageNamed:[[_dic objectForKey:str] objectAtIndex:indexPath.row][name]];
? ? cell.label.text=name;
? ? cell.backgroundColor = [UIColor redColor];
? ? returncell;
}
//設(shè)置頭視圖 和尾視圖
-(UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{
? ? if([kind isEqualToString:UICollectionElementKindSectionHeader]){
?? ? ? staticNSString*cellId =@"header";
? ? ? ? HeaderCollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:cellId forIndexPath:indexPath];
? ? ? ? headerCell.label.text= [NSStringstringWithFormat:@"第%ld組",indexPath.section+1];
? ? ? ? headerCell.backgroundColor = [UIColor yellowColor];
? ? ? ? returnheaderCell;
? ? }else{
? ? ? ? staticNSString*cellId =@"footer";
? ? ? ? FooterCollectionReusableView*footerCell? = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellId forIndexPath:indexPath];
? ? ? ? footerCell.label.text=@"結(jié)尾";
? ? ? ? footerCell.backgroundColor = [UIColor greenColor];
? ? ? ? returnfooterCell;
? ? }
}
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
? ? NSLog(@"第%ld分區(qū)---第%ld行",indexPath.section,indexPath.row);
? ? NewViewController? *new = [[NewViewController alloc]init];
? ? NSString *str = _dic.allKeys[indexPath.section];
? ? NSString *name = [[[_dic objectForKey:str]objectAtIndex:indexPath.row] allKeys][0];
? ? new.name= name;
? ? new.img = [UIImage imageNamed:[[_dic objectForKey:str] objectAtIndex:indexPath.row][name]];
? ? [self presentViewController:new animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end
#import "NewViewController.h"
@interface NewViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end
@implementationNewViewController
-(UIImageView*)imageView{
? ? if (!_imageView) {
? ? ? ? _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
? ? }
? ? return _imageView;
}
-(UILabel*)label{
? ? if(!_label) {
? ? ? ? _label = [[UILabel alloc]initWithFrame:CGRectMake(200, self.view.frame.size.height-40, 200, 30)];
? ? }
? ? return _label;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self.view addSubview:self.imageView];
? ? [self.view addSubview:self.label];
? ? self.imageView.image = self.img;
? ? self.label.text = self.name;
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
? ? [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
#import
@interfaceMyCollectionViewCell :UICollectionViewCell
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end
#import "MyCollectionViewCell.h"
@implementationMyCollectionViewCell
-(UIImageView*)imageView{
? ? if (!_imageView) {
? ? ? ? _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 50)];
? ? }
? ? return _imageView;
}
-(UILabel*)label{
? ? if(!_label) {
? ? ? ? _label = [[UILabel alloc]initWithFrame:CGRectMake(30, 70, 80, 20)];
? ? }
? ? return _label;
}
-(instancetype)initWithFrame:(CGRect)frame{
? ? if(self= [superinitWithFrame:frame]) {
? ? ? ? [self addSubview:self.imageView];
? ? ? ? [self addSubview:self.label];
? ? }
? ? return self;
}
@end