@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
NSArray *array,*array1;
UICollectionView *collection;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置布局流
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//设置item大小
layout.itemSize=CGSizeMake(100, 100);
//设置最小行间距
layout.minimumLineSpacing=10;
//设置最小列间距
layout.minimumInteritemSpacing=10;
//设置滚动方向
layout.scrollDirection=UICollectionViewScrollDirectionVertical;
//初始化网格
collection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300) collectionViewLayout:layout];
collection.delegate=self;
collection.dataSource=self;
collection.backgroundColor=[UIColor blueColor];
[self.view addSubview:collection];
array=@[@"20.jpg",@"21.jpg",@"22.jpg",@"23.jpg",@"24.jpg"];
array1=@[@"行",@"kjas",@"jkasd",@"oiapsd",@"nkdas"];
//注册单元格
[collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"my"];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 5;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *coll=[collectionView dequeueReusableCellWithReuseIdentifier:@"my" forIndexPath:indexPath];
coll.image.image=[UIImage imageNamed:array[indexPath.row]];
coll.lable.text=array1[indexPath.row];
return coll;
}