记得设置代理,记得设置代理,记得设置代理.....
方法1.设置行间距
-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
if (section == 1) {
//设置每个cell之间的行间距为5
return 5;
}
return 0;
}
//设置列间距
-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
if (section == 1) {
return 2;
}
return 0;
}
方法二:设置行间距和列间距
//1:初始化collection时设置
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView.collectionViewLayout = layout;
layout.minimumLineSpacing = 1;//设置最小行间距
layout.minimumInteritemSpacing = 2;//item间距(最小值)
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置section的编距
//2: 设置section间的大小
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if (section == 0 )
{
//上左下右距离(好像是)
return UIEdgeInsetsMake(0, 0, 0, 0);
}
return UIEdgeInsetsMake(10 * widthPropor, 0, 0, 0);
}
//3: 通过代理方法,设置独立item的大小
- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return CGSizeMake(88,77 );
}
#pragma mark-设置section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 3;
}
//设置row个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}