下面粘代码
-(void)setupUI{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
self.layout = layout;
self.layout.minimumLineSpacing = 5;
self.layout.minimumInteritemSpacing = 5;
CGFloat itemWidth = (CGRectGetWidth([UIScreen mainScreen].bounds))/3;
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.layout.itemSize = CGSizeMake(itemWidth, 150);
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:self.layout];
self.collectionView = collectionView;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[collectionView registerClass:[LSCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
[collectionView registerClass:[BottomCell class] forCellWithReuseIdentifier:@"bottomCellID"];
}
#pragma mark -collectionView的数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if(section == 1){
self.layout.itemSize = CGSizeMake(ScreenW, 150);
return 2;
}
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
LSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
cell.indexPath = indexPath;
return cell;
}
BottomCell *bottomCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bottomCellID" forIndexPath:indexPath ];
return bottomCell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reuseView;
if (kind == UICollectionElementKindSectionHeader) {
reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
reuseView.backgroundColor = [UIColor redColor];
}else{
reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
reuseView.backgroundColor = [UIColor blueColor];
}
return reuseView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"这是第%ld组,第%ld个item",indexPath.section,indexPath.row);
}
//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 15, 5, 15);//分别为上、左、下、右
}
#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return CGSizeMake((ScreenW - 60) / 3,150);
}
return CGSizeMake(ScreenW, 120);
}
网友评论