这是一个实现瀑布流效果的 CollectionViewFlowLayout,它还支持多节。
您可以自定义头部和尾部,也可以自定义 UICollectionViewCell。
现在,在 0.5 版本中,您可以在滑动 collectionView 时设置 header 保持在顶部。
##安装
或者,您可以直接将文件从 DDCollectionViewFlowLayout / Classes
拖拽到您的项目中。
要运行示例项目,请先克隆仓库,然后从项目目录中运行 pod install
。
1.示例:类似微信照片墙效果
DDCollectionViewFlowLayout *layout = [[DDCollectionViewFlowLayout alloc] init];
layout.delegate = self;
layout.enableStickyHeaders = YES; //set the header sticky if you want
[self.collectionView setCollectionViewLayout:layout];
实现 DDCollectionViewDelegateFlowLayout & UICollectionViewDataSource
@required 或 @optional 方法
DDCollectionViewDelegateFlowLayout
继承自 UICollectionViewDelegateFlowLayout
协议。
代码
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [[dataDict allKeys] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
sortedArray = [[dataDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
return [dataDict[sortedArray[section]] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(DDCollectionViewFlowLayout *)layout numberOfColumnsInSection:(NSInteger)section{
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell *cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
NSURL *url = dataDict[sortedArray[indexPath.section]][indexPath.row];
[_assetLibrary assetForURL:url
resultBlock:^(ALAsset *asset) {
[cell.photo setImage:[UIImage imageWithCGImage:asset.thumbnail]];
}
failureBlock:^(NSError *error) {
}];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if(kind == UICollectionElementKindSectionHeader){
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
// header.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f];
UILabel *lblTitle = (UILabel *)[header viewWithTag:2];
lblTitle.text = sortedArray[indexPath.section];
return header;
}
return nil;
}
#pragma mark - UICollectionView Delegate Methods
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 1;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(1, 1, 1, 1);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(80, 80);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(self.view.bounds.size.width, 30);
}
2.示例:瀑布流效果
示例代码
#pragma mark - UICollectionView DataSource Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return dataList.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(DDCollectionViewFlowLayout *)layout numberOfColumnsInSection:(NSInteger)section{
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
WaterfallCell *cell = (WaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
ALAsset *set = dataList[indexPath.item];
[cell.photo setImage:[UIImage imageWithCGImage:set.thumbnail]];
return cell;
}
#pragma mark - UICollectionView Delegate Methods
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 5;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100 + indexPath.item % 20);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(DDCollectionViewFlowLayout *)layout numberOfColumnsInSection:(NSInteger)section;
DDCollectionViewDelegateFlowLayout
继承自 UICollectionViewDelegateFlowLayout
协议。所以您可以在 DDCollectionViewDelegateFlowLayout
中使用所有 UICollectionViewDelegateFlowLayout
协议的方法。
DeJohn Dong, [email protected]
DDCollectionViewFlowLayout 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。