UICollectionView 简单使用

- (void)initCollectView
{
    if (!_collectView) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(5,44, kSCREN_BOUNDS.size.width - 10, kSCREN_BOUNDS.size.height - 10 - 44 - 64) collectionViewLayout:flowLayout];
        _collectView.alwaysBounceVertical  =YES;
        _collectView.userInteractionEnabled = YES;
        _collectView.backgroundColor = kColor_White;
        _collectView.delegate = self;
        _collectView.dataSource = self;
        [_collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];


        UINib *headerNib = [UINib nibWithNibName:NSStringFromClass([ZXPhotoHeaderCollectionReusableView class])  bundle:[NSBundle mainBundle]];
        [_collectView registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
        //    [collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
        //获取布局
        flowLayout = (UICollectionViewFlowLayout *)_collectView.collectionViewLayout;
        //设置属性
        flowLayout.itemSize = CGSizeMake(100, 100);
        //设置内边距
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5,0);
//        flowLayout.sectionInset = UIEdgeInsetsZero;

        [self.view addSubview:_collectView];
    }

}
#pragma mark -  UICollectionViewDelegate &&dataSource
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{

    return 5;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kSCREN_BOUNDS.size.width - 10, 54);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    ZXPhotoHeaderCollectionReusableView *reusableView = (ZXPhotoHeaderCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    reusableView.tag = indexPath.section + 100;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeadView:)];
    [reusableView addGestureRecognizer:tap];

    AllServiceEffectRes *serviceRes = self.photoDatas[indexPath.section];
    reusableView.nameLab.text = serviceRes.ServiceName;
    reusableView.dateLab.text = serviceRes.TGStartTime;
    reusableView.accImageView.image = [UIImage imageNamed:@""];

    return reusableView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.photoDatas.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    AllServiceEffectRes *serviceRes = self.photoDatas[section];
    return serviceRes.ImageEffects.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    cell.userInteractionEnabled = YES;
    cell.contentView.userInteractionEnabled = YES;
    cell.backgroundColor = [UIColor redColor];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.backgroundColor = [UIColor orangeColor];
    imageView.tag = 10086;
    imageView.userInteractionEnabled = YES;
    [cell.contentView addSubview:imageView];

    AllServiceEffectRes *serviceRes = self.photoDatas[indexPath.section];
    AllServiceEffectImageRes *imageRes = serviceRes.ImageEffects[indexPath.row];
    [imageView setImageWithURL:[NSURL URLWithString:imageRes.ThumbnailURL] placeholderImage:[UIImage imageNamed:@"People-default"]];



    return cell;

}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DLOG(@"didSelectItemAtIndexPathdidSelectItemAtIndexPathdidSelectItemAtIndexPath");
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    UIImageView *imageView = [cell viewWithTag:10086];
    AllServiceEffectRes *serviceRes = self.photoDatas[indexPath.section];
    AllServiceEffectImageRes *imageRes = serviceRes.ImageEffects[indexPath.row];
    UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageRes.OriginalImageURL]]];
    [SJAvatarBrowser showImage:imageView img:image];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值