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;
}
相關文章
相關標籤/搜索