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

简单研究了一下collection,


对用的类应遵守 以下协议

<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

- (void)viewDidLoad
{
  
    [super viewDidLoad];
    //注册cell
    [self.collectionView registerClass:[collectionCell class] forCellWithReuseIdentifier:@"<pre name="code" class="objc">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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值