UICollectionViewFlowLayout懒加载
- (UICollectionViewFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
//每个item大小
_flowLayout.itemSize = CGSizeMake(165, 250);
//间距
// CGFloat spacing = (kScreenWidth - 300) / 4.0;
//最小行间距
_flowLayout.minimumLineSpacing = 15;
//最小列间距
_flowLayout.minimumInteritemSpacing = 30;
//上下左右四个边缘的间距
//每个区的上下左右
_flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
}
return _flowLayout;
}
UICollectionView懒加载
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor clearColor];
//代理
_collectionView.dataSource = self;
_collectionView.delegate = self;
}
return _collectionView;
}
注册item
//注册item
[self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:@"Cell"];
代理方法
签订协议<UICollectionViewDataSource, UICollectionViewDelegate>
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSource.count;
}
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
//item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
//获取cell对应的图片
NSDictionary *URLDic = self.dataSource[indexPath.item];
//获取字典内的图片网址
NSString *imageURL = URLDic[@"thumbURL"];
NSString *imageWidth = [NSString stringWithFormat:@"%@", URLDic[@"width"]];
cell.widthLabel.text = imageWidth;
//居中
cell.widthLabel.textAlignment = NSTextAlignmentCenter;
NSURL *imageurl = [NSURL URLWithString:imageURL];
//获取图片
[cell.imageView sd_setImageWithURL:imageurl placeholderImage:[UIImage imageNamed:@"placeholder"]];
//图片失真解决方法
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
//item点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"点击");
}
注册分区头和分区尾
//注册分区头
[self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//注册分区尾
[self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
分区头分区尾代理方法
//分区尾大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(0, 30);
}
//分区头大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(0, 30);
}
//分区头部尾部协议方法
//分区头部尾部共用一个协议方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//获取分区头是从重用队列去取的,而不是alloc init 的的
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor lightGrayColor];
UILabel *headerLabel = [[UILabel alloc] init];
headerLabel.frame = CGRectMake(0, 0, 150, 30);
headerLabel.text = @"header--美女";
headerLabel.backgroundColor = [UIColor redColor];
[headerView addSubview:headerLabel];
return headerView;
}else {
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor lightGrayColor];
UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
footerLabel.text = @"footer--漂亮";
footerLabel.backgroundColor = [UIColor greenColor];
[footerView addSubview:footerLabel];
return footerView;
}
}