网格

@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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容