研究了一下collectionView ,总结一下,防止忘记

简单研究了一下collection,


对用的类应遵守 以下协议

,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

- (void)viewDidLoad
{
  
    [super viewDidLoad];
    //注册cell
    [self.collectionView registerClass:[collectionCell class] forCellWithReuseIdentifier:@"
colcellID
"]; //注册 [self.collectionView registerClass:[collectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"
headID
"];}
 
  
-(void)addCollectionView{
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout: flowLayout];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.contentInset = UIEdgeInsetsMake(15, 0, 30,3);
    collectionView.delegate = self;
    collectionView.dataSource = self;
    //系统默认竖直滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //各个单元格大小
    flowLayout.itemSize = CGSizeMake(90, 40);
    //单元格上下左右间距大小
    flowLayout.sectionInset = UIEdgeInsetsMake(7.5f, 0, 7.5f, 0);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 6;
    //最小行间距
    flowLayout.minimumLineSpacing = 6;
    //顶部视图 如果没有sectionHeader视图的话,这行代码注释掉
    flowLayout.headerReferenceSize = CGSizeMake(collectionView.width, 40);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.foodList.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
   
    return unit.data.count;
}


#pragma mark - 顶部控件
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    collectionHeadView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headID forIndexPath:indexPath];
    return headView;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    LOG(@"%@",indexPath);
    collectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:colcellID forIndexPath:indexPath];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //点击
    
}

-----2016.08.01补充-----

//设置每个section中cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat showWidth = SCREEN_WIDTH - 80 - 20;
    
    if (indexPath.section == 0) {
        return CGSizeMake(showWidth, 81);
    }else if (indexPath.section == 1){
        return CGSizeMake(showWidth, 66);
    }else{
        return CGSizeMake(showWidth / 2, 132);
    }
}


你可能感兴趣的:(iOS)