1.创建一种布局
UICollectionViewFlowLayout *flowL = [[UICollectionViewFlowLayout alloc]init];
//设置每一个item的大小
flowL.itemSize = CGSizeMake((view2.frame.size.width - 15-20) / 3 , (view2.frame.size.width - 15-20) /3 );
flowL.sectionInset = UIEdgeInsetsMake(3, 0, 3, 0);
//列
flowL.minimumInteritemSpacing = 2;
//行最小间距,低于该值就会另起一行
flowL.minimumLineSpacing = 2;
2.创建集合视图
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 32, ScreenWidth-40,100) collectionViewLayout:flowL];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.scrollEnabled = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
//注册对应的cell
[self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
3.UICollectionViewDelegate,UICollectionViewDataSource协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (_photoArrayM.count == 0) {
return 0;
}
else{
return _photoArrayM.count;
}
}
//返回每一个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
MLSelectPhotoAssets *asset = self.photoArrayM[indexPath.item];
cell.photoView.image = [MLSelectPhotoPickerViewController getImageWithImageObj:asset];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
SelectedPictureViewController *sVC = [[SelectedPictureViewController alloc]init];
[sVC returnControl:^(BOOL deleteImage) {
if (deleteImage) {
[self.photoArrayM removeObjectAtIndex:indexPath.row];
}
}];
sVC.asset = self.photoArrayM[indexPath.row];
//NSLog(@"%ld",(long)indexPath.item);
[self.navigationController pushViewController:sVC animated:YES];
// MLSelectPhotoBrowserViewController *browserVC = [[MLSelectPhotoBrowserViewController alloc]init];
// browserVC.currentPage = indexPath.item;
// browserVC.photos = self.photoArrayM;
// //browserVC.isEditing = YES;
// [self.navigationController pushViewController:browserVC animated:YES];
}
4. UICollectionViewDelegateFlowLayout协议。
//设定指定Cell的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return (CGSize){ScreenWidth/3,ScreenWidth/3+30};
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
//设定指定区内Cell的最小行距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 1;
}
//设定指定区内Cell的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}