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.效果图

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值