網(wǎng)格試圖

#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 <UIKit/UIKit.h> @interface NewViewController : UIViewController @property(nonatomic,strong)UIImage *img; @property(nonatomic,strong)NSString *name; @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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拒啰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子完慧,更是在濱河造成了極大的恐慌谋旦,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屈尼,死亡現(xiàn)場離奇詭異册着,居然都是意外死亡,警方通過查閱死者的電腦和手機脾歧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門甲捏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人涨椒,你說我怎么就攤上這事摊鸡。” “怎么了蚕冬?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵免猾,是天一觀的道長。 經(jīng)常有香客問我囤热,道長猎提,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任旁蔼,我火速辦了婚禮锨苏,結(jié)果婚禮上疙教,老公的妹妹穿的比我還像新娘。我一直安慰自己伞租,他們只是感情好贞谓,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著葵诈,像睡著了一般裸弦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上作喘,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天理疙,我揣著相機與錄音,去河邊找鬼泞坦。 笑死窖贤,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的贰锁。 我是一名探鬼主播赃梧,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼李根!你這毒婦竟也來了槽奕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤房轿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后所森,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囱持,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年焕济,在試婚紗的時候發(fā)現(xiàn)自己被綠了纷妆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡晴弃,死狀恐怖掩幢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情上鞠,我是刑警寧澤际邻,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站芍阎,受9級特大地震影響世曾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谴咸,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一轮听、第九天 我趴在偏房一處隱蔽的房頂上張望骗露。 院中可真熱鬧,春花似錦血巍、人聲如沸萧锉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽驹暑。三九已至,卻和暖如春辨赐,著一層夾襖步出監(jiān)牢的瞬間优俘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工掀序, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留帆焕,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓不恭,卻偏偏與公主長得像叶雹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子换吧,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容