首页 > 代码库 > UICollectionView详解

UICollectionView详解

今天,将和大家一起学习UICollectionView,UIcollectionView自出来后,一直受追捧,确实好用。今天有朋友问我如何添加heardView,我简单的回答:tableview如何添加,那么CollectionView就怎么添加,后来经过自己实验发现确实不是那回事,所以列出一些自己犯的错误,供大家参考。

1.首先实例化一个 UICollectionViewFlowLayout因为要设置item,itemSize,等一些属性。

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(100,50); //CGSizeMake(200 , 200);
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.minimumLineSpacing = 5;



2.实例化一个UICollectionView

myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.height, self.view.frame.size.width) collectionViewLayout:layout];
    
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    
    myCollectionView.backgroundColor = [UIColor blueColor];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewIdentifier"];
     [myCollectionView registerClass:[CollectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.view addSubview:myCollectionView];



3.设置数据源和代理方法

#pragma mask - dataSource

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewIdentifier" forIndexPath:indexPath];
    for (UIView*view in cell.contentView.subviews) {
        if (view) {
            [view removeFromSuperview];
        }
    }
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
    lab.backgroundColor = [UIColor yellowColor];
    lab.text = [NSString stringWithFormat:@"%d",[indexPath row]+[indexPath section]*3];
    cell.backgroundColor = [UIColor redColor];
//    cell.backgroundView.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:lab];
    UIView *view = [[UIView alloc] initWithFrame:cell.bounds];
    [view setBackgroundColor:[UIColor whiteColor]];
    cell.selectedBackgroundView = view;


    return cell;
    
};

//cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(50, 50);
}



-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
};
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 10;
};

#pragma mask - delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击%@",indexPath);
    
}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}


4.设置heardView,hearView需要自定义,并且继承


5.自定义完成后,需要在代理方法中从缓存池中找到已经注册的heardView

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    CollectionHeadView *heard = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    
    heard.lable.text = [NSString stringWithFormat:@"第%d组",indexPath.section];
    return heard;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(320, 40);
}

6.效果图

大家按照代码一步一步来,肯定就是超简单。