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的實現代碼。

相關文章
相關標籤/搜索