码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectionView创建瀑布流

时间:2015-10-13 00:22:41      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

UICollectionView与UITableView类似,它们的用法也差不多,所有的实现都是通过delegate以及dataSource来完成的,都有几个必须实现的协议,为以下协议。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER forIndexPath:indexPath];
    NSInteger imageIndex = arc4random()%10;
    NSString *imageName = [NSString stringWithFormat:@"00%ld.jpg", imageIndex];
    
    [cell.imageView setImage:[UIImage imageNamed:imageName]];
    [cell setImageViewFrame:cell.frame];
    
    return cell;
}

当然,仅仅这样还是无法实现我们需要的效果,因此,我们就需要用到另一个类了,它便是
UICollectionViewLayout,它是一个对collectionview进行布局的类。它有个系统自带的子类UICollectionViewFlowLayout,如果你只需要一个简单的效果,那么系统自带的子类便可以实现我们需要的效果。因此,我们需要创建它的一个子类LSCollectionViewLayout,为此我们需要重写几个他的方法。
-(void)prepareLayout;

-(CGSize) collectionViewContentSize;

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;
1、创建LSCollectionViewLayout对象时会自动调用
2、设置内容视图的大小(类似
UIScrollView的contentSize
3、返回一个数组,数组中包含了每个item的描述。
4、设置每个item的描述。
5、设置每次item改变是是否会有动画。

在第四个方法中,我们需要用到另一个用于描述的类
UICollectionViewLayoutAttributes,每一个item都对应一个UICollectionViewLayoutAttributes的对象。它设置了item的许多属性。
@property (nonatomic) CGRect frame;
@property (nonatomic) CGPoint center;
@property (nonatomic) CGSize size;
@property (nonatomic) CATransform3D transform3D;
@property (nonatomic) CGRect bounds NS_AVAILABLE_IOS(7_0);
@property (nonatomic) CGAffineTransform transform NS_AVAILABLE_IOS(7_0);
@property (nonatomic) CGFloat alpha;
@property (nonatomic) NSInteger zIndex; // default is 0
@property (nonatomic, getter=isHidden) BOOL hidden; // As an optimization, UICollectionView might not create a view for items whose hidden attribute is YES
@property (nonatomic, retain) NSIndexPath *indexPath;

@property (nonatomic, readonly) UICollectionElementCategory representedElementCategory;
@property (nonatomic, readonly) NSString *representedElementKind; // nil when representedElementCategory is UICollectionElementCategoryCell

我们可以通过这些来对collectionview的item进行布局。

以下为主要的代码

-(void)prepareLayout
{
    [super prepareLayout];
    
    _cellMinHeight = _cellMinHeight == 0 ? 100:_cellMinHeight;
    _cellMaxHeight = _cellMaxHeight == 0 ? 200:_cellMaxHeight;
    _padding = _padding == 0 ? 5:_padding;
    _spaceing = _spaceing == 0 ? 5:_spaceing;
    _lineCount = _lineCount == 0 ? 2:_lineCount;
    _numberOfCellsInSections = [self.collectionView numberOfItemsInSection:0];
    
    [self initCellWeith];
    [self initCellHeight];
    
}



-(CGSize) collectionViewContentSize
{
    CGFloat height = [self maxCellYArrayWithArray:self.cellYarray];
    return CGSizeMake(SCREEN_MAX_WEITH, height);
}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    [self initCellYarray];
    NSMutableArray * array = [NSMutableArray new];
    
    for (int i = 0 ; i < _numberOfCellsInSections ; i ++)
    {
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [array addObject:attributes];
    }
    
    return array;
}


-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewLayoutAttributes * attritubes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGRect fram = CGRectZero;
    
    CGFloat cellHeight = [_cellHeightArray[indexPath.row] floatValue];
    
    NSInteger minYindex = [self minCellYArrayWithArray:self.cellYarray];

    CGFloat tempX = [self.cellXarray[minYindex] floatValue];
    
    CGFloat tempY = [self.cellYarray[minYindex] floatValue];
    
    fram = CGRectMake(tempX, tempY, _cellWeith, cellHeight);
    
    self.cellYarray[minYindex] = @(tempY + cellHeight + self.spaceing);
        
    attritubes.frame = fram;

    
    return attritubes;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return NO;
}

 

这便是一个简单的瀑布流了,demo下载

http://d.cocoachina.com/code/detail/327027

 

 

 






UICollectionView创建瀑布流

标签:

原文地址:http://www.cnblogs.com/lsios/p/4873295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!