UICollectionViewFlowLayout懶加載
- (UICollectionViewFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
//每個(gè)item大小
_flowLayout.itemSize = CGSizeMake(165, 250);
//間距
// CGFloat spacing = (kScreenWidth - 300) / 4.0;
//最小行間距
_flowLayout.minimumLineSpacing = 15;
//最小列間距
_flowLayout.minimumInteritemSpacing = 30;
//上下左右四個(gè)邊緣的間距
//每個(gè)區(qū)的上下左右
_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;
}
注冊(cè)item
//注冊(cè)item
[self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:@"Cell"];
代理方法
簽訂協(xié)議<UICollectionViewDataSource, UICollectionViewDelegate>
//item個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSource.count;
}
//分區(qū)數(shù)
- (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對(duì)應(yīng)的圖片
NSDictionary *URLDic = self.dataSource[indexPath.item];
//獲取字典內(nèi)的圖片網(wǎng)址
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點(diǎn)擊方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"點(diǎn)擊");
}
注冊(cè)分區(qū)頭和分區(qū)尾
//注冊(cè)分區(qū)頭
[self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//注冊(cè)分區(qū)尾
[self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
分區(qū)頭分區(qū)尾代理方法
//分區(qū)尾大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(0, 30);
}
//分區(qū)頭大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(0, 30);
}
//分區(qū)頭部尾部協(xié)議方法
//分區(qū)頭部尾部共用一個(gè)協(xié)議方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//獲取分區(qū)頭是從重用隊(duì)列去取的雹顺,而不是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;
}
}