- (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];
}
UICollectionView 简单使用
最新推荐文章于 2024-10-16 11:58:31 发布