記得設(shè)置代理,記得設(shè)置代理,記得設(shè)置代理.....
方法1.設(shè)置行間距
? ? ?-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? if (section == 1) {
? ? ? ? ? ? ? ? ? ? ?//設(shè)置每個(gè)cell之間的行間距為5
? ? ? ? ? ? ? ? ? ? ?return 5;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? return 0;
? ? ? }
//設(shè)置列間距
-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
??????? if (section ==? 1) {
??????????? return 2;
??????? }
??????? return 0;
}
方法二:設(shè)置行間距和列間距
//1:初始化collection時(shí)設(shè)置
? ? ?UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
? ? self.collectionView.collectionViewLayout = layout;
? ? layout.minimumLineSpacing = 1;//設(shè)置最小行間距
? ? layout.minimumInteritemSpacing = 2;//item間距(最小值)
? ? layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//設(shè)置section的編距
//2: 設(shè)置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: 通過(guò)代理方法,設(shè)置獨(dú)立item的大小
- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
? ? ? return CGSizeMake(88,77 );
}
#pragma mark-設(shè)置section個(gè)數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
? ? ?return 3;
}
//設(shè)置row個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
???? return 1;
}