網(wǎng)格

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

//初始化網(wǎng)格

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;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容