UICollectionView

UICollectionView使用(一)git

因为项目须要用到UICollectionView控件,因此本人自行到网络上查找相关的资料进行学习;如下是本人的理解。github

(一)collectionView内容的填充;网络

关键是实现UICollectionViewDataSource,UICollectionViewDelegate的如下几个协议app

(1)协议实现函数

//设定collectionView某个section的cell数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

//设定cell的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    return cell;
}

//点击cell
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击操做");
}

实现这几个函数就基本够用了。布局


(二)UICollectionView布局学习

(1)系统线性的布局
spa

UICollectionViewFlowLayoutcode

(2)自定义布局orm

建立UICollectionViewLayout子类,重写下面函数:

-(void)prepareLayout //建立layout前作一些准备工做

-(CGSize)collectionViewContentSize //设置collectionView的contentSize

-(BOOL)shouldInvalidateLayoutForBoundsChange:

-(UICollectionViewLayoutAttributes *)layoutAttributesForElementsInRect: //设置全部cell的属性

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath: //设定单个cell的属性

注意:版本更新后initialLayoutAttributesForAppearingItemAtIndexPath、finalLayoutAttributesForDisappearingItemAtIndexPath已经做废,因此须要使用prepareForCollectionViewUpdates来处理添加或删除的操做。

//更新前保存需改变item的indexPath
-(void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems {
    
    [super prepareForCollectionViewUpdates:updateItems];
    
    self.deleteItemsArray = [NSMutableArray array];
    self.insertItemsArray = [NSMutableArray array];
    
    for (UICollectionViewUpdateItem *update in updateItems) {
        
        if (update.updateAction == UICollectionUpdateActionInsert) {
            
            [self.insertItemsArray addObject:update.indexPathAfterUpdate];
        }
        
        
        else if (update.updateAction == UICollectionUpdateActionDelete) {
            
            [self.deleteItemsArray addObject:update.indexPathBeforeUpdate];
        }
    }
}

//更新后清除以前保存的indexPath
-(void)finalizeCollectionViewUpdates {
    
    self.insertItemsArray = nil;
    self.deleteItemsArray = nil;
}

//这个函数是做用整个collectionView的item,因此那些item须要改变attributes,须要作一个判断。
//这个是做用于添加item的
-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
    
    //必须执行super
    UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
    
    
    if ([self.insertItemsArray containsObject:itemIndexPath]) {
        
        if (!attributes) {
            attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
        }
        
        //这个位置能够设定改变后的attributes,以下
        
        attributes.alpha = 0.0;
        attributes.center = CGPointMake(_center.x, _center.y);
    }
    
    return attributes;
}


//这个函数是做用整个collectionView的item,因此那些item须要改变attributes,须要作一个判断。
//这个是做用于删除item的
-(UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
    
    //必须执行super
    UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
    
    
    if ([self.deleteItemsArray containsObject:itemIndexPath]) {
        
        if (!attributes) {
            attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
        }
        
        //这个位置能够设定改变后的attributes,以下
        
        attributes.alpha = 0.0;
        attributes.center = CGPointMake(_center.x, _center.y);
        attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
    }
    
    return attributes;
}

具体的操做可参照CircleLayout的实现代码。

相关文章
相关标签/搜索