UICollectionView的简单使用和经常使用代理方法

UICollectionView相对于UITableView有更加自由的布局,作出的界面可变性更大最近开始接触使用UICollectionView,整理了一下经常使用的代理方法布局

 

首先须要先添加UICollectionView的代理:UICollectionViewDelegate   UICollectionViewDataSource  UICollectionViewDelegateFlowLayoutspa

 

在viewdidLoad的时候注册UICollectionView 的cell代理

UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];

须要须要添加headerView或者footerView一样须要注册code

[collection registerClass:[collectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];//collectionHeaderView是自定义的view

想要定义错两3c提哦弄View的UI就须要在它的代理方法中进行设置就行blog

//返回headerView的大小为宽320  高 100
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeMake(320, 100);

    return size;
}

定义须要显示的headerView UI,由于UICOllectionView的headerView是复用的,因此须要像使用Cell同样在代理方法中自定义ci

//返回headerView
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
        if (kind == UICollectionElementKindSectionHeader)
        {
                VoteTableHeaderView *myHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
                   //在这里进行headerView的操做
            reusableview = myHeaderView;
    }
    
    return reusableview;
}    
//返回section 的数量
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//返回对应section的item 的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

       return [self.myDataArr count];
}
//建立和复用cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //重用cell
    MineVoteTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MIneVoteCell" forIndexPath:indexPath];
    //赋值给cell
    
    return cell;
}
//定义每一个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.width/2, 150);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
//每一个section中不一样的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //在这里进行点击cell后的操做
}
//每一个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
相关文章
相关标签/搜索