UICollectionView
2017-01-06 本文已影响7人
lym不解释
创建
#pragma mark - 初始化CollectionView
- (void)settingCollectionView {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
CGFloat kSectionMargin = W(5);
//每行3个Cell
CGFloat screenWith = self.view.bounds.size.width;
CGFloat cellWidth = (screenWith - 4 *kSectionMargin)/3 ;
//定义每个item 的大小
flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth*140/118);
//头部大小
flowLayout.headerReferenceSize = CGSizeMake(screenWith, SCREEN_WIDTH*288/375);
//定义每个item 横向的间距
flowLayout.minimumLineSpacing = kSectionMargin;
//定义每个item 纵向的间距
flowLayout.minimumInteritemSpacing = kSectionMargin;
//定义每个item 的边距距 //上左下右
flowLayout.sectionInset = UIEdgeInsetsMake(kSectionMargin, kSectionMargin, kSectionMargin, kSectionMargin);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) collectionViewLayout:flowLayout];
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
}
代理
#pragma mark- UICollectionViewDelegate\UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
dataModel *model = self.dataArr[indexPath.row];
NSString *urlStr;
if (model.imgUrl.count) {
urlStr = model.imgUrl[0];
}
UIImageView *cellImageV = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
[cellImageV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"占位图"]];
cellImageV.contentMode = UIViewContentModeScaleToFill;
[cell.contentView addSubview:cellImageV];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
dataModel *model = self.dataArr[indexPath.row];
DetailsController *vc = [[DetailsController alloc] init];
vc.hidesBottomBarWhenPushed = YES;
vc.ClassId = model.findImgDetails_id;
[self.navigationController pushViewController:vc animated:YES];
}
// 头部或者尾部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerRV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
[headerRV.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[headerRV addSubview:_bannerView];
return headerRV;
}else //尾部视图
{
return nil;
}
}