用UICollectionView实现瀑布流
来源:互联网 发布:小米盒子视频软件 编辑:程序博客网 时间:2024/05/21 23:59
瀑布流的实现要重写UICollectionViewLayout来实现:主要是重写以下几个方法:
-(void)prepareLayout;//开始布局
-(CGSize)collectionViewContentSize;//内容大小
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;//每个cell的属性列表
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath//单个的属性
具体的实现:
@protocol WaterFLayoutDelegate<NSObject>
@required
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath;
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
@end
@interface MyLayout :UICollectionViewLayout
{
float x;
float leftY;
float rightY;
}
@property(nonatomic,assign)UIEdgeInsets sectionInset;
@property(nonatomic,weak)id<WaterFLayoutDelegate> delegate;
@end
@implementation MyLayout
-(void)prepareLayout
{
[superprepareLayout];
self.delegate=(id<WaterFLayoutDelegate>)self.collectionView.delegate;
self.sectionInset=[self.delegatecollectionView:self.collectionViewlayout:selfinsetForSectionAtIndex:0];
}
-(CGSize)collectionViewContentSize
{
returnCGSizeMake(self.collectionView.frame.size.width,MAX(leftY,rightY));
}
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
x=0;
leftY=0;
rightY=0;
NSMutableArray *attributes=[NSMutableArrayarray];
NSInteger section=self.collectionView.numberOfSections;
for (int i=0; i<section; i++) {
NSInteger allCount=[self.collectionViewnumberOfItemsInSection:i];
for (int j=0; j<allCount; j++) {
NSIndexPath *indexPath=[NSIndexPathindexPathForRow:j inSection:i];
[attributesaddObject:[selflayoutAttributesForItemAtIndexPath:indexPath]];
}
}
return attributes;
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSAssert([self.delegaterespondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)],@"collectionView:layout:sizeForItemAtIndexPath not exist");
CGSize itemSize=[self.delegatecollectionView:self.collectionViewlayout:selfsizeForItemAtIndexPath:indexPath];
UICollectionViewLayoutAttributes *attributes=[UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];
if (leftY<=rightY) {
x=self.sectionInset.left;
leftY+=self.sectionInset.top;
attributes.frame=CGRectMake(x,leftY, itemSize.width,itemSize.height);
leftY+=itemSize.height;
}else{
x=itemSize.width+self.sectionInset.left*3;
rightY+=self.sectionInset.top;
attributes.frame=CGRectMake(x,rightY, itemSize.width, itemSize.height);
rightY+=itemSize.height;
}
return attributes;
}
@end
- 用UICollectionView实现瀑布流
- UICollectionView实现瀑布流
- UICollectionView的瀑布流实现
- ios 应用UICollectionView实现瀑布流
- ios 应用UICollectionView实现瀑布流
- iOS UICollectionView实现瀑布流(3)
- 利用UICollectionView 实现 页面瀑布流效果
- iOS UICollectionView实现瀑布流(3)
- iOS 使用UICollectionView实现瀑布流
- UICollectionView瀑布流的实现,带例子
- 使用UICollectionView 重写UICollectionViewLayout实现瀑布流
- iOS开发-UICollectionView实现瀑布流
- Swift:用UICollectionView整一个瀑布流
- UICollectionView瀑布流制作
- iOS UICollectionView瀑布流
- UICollectionView 瀑布流 初级
- UICollectionView之瀑布流
- 瀑布流UICollectionView
- webview的具体设置与使用
- android横竖全屏设置
- Java 8/25
- 【HDU4072】【线段树】【确定上下界】
- stl::string的引用计数
- 用UICollectionView实现瀑布流
- linux时间和定时脚本
- IP地址、子网掩码与划分方法
- jst中,if的判断条件
- Android开发之如何保证Service不被杀掉(broadcast+system/app)
- 我的git笔记
- java集合类线程安全
- idea自动生成serialVersionUID
- C++输入