iOS自定義collectionViewLayout

iOS自定義collectionViewLayout

iOS開發(fā)collectionView也算用的比較廣泛了缝彬,但是系統(tǒng)的collectionViewLayout有時候不能夠滿足開發(fā)需求,比如小紅書那種瀑布流没佑。這里講一講自定義collectionViewLayout,需要重寫四個方法

  1. -(void)prepareLayout
    2.-(CGSize)collectionViewContentSize;
    3.-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
    4.-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

直接上代碼

@interface SSCollectionViewLayout()

/** cell height arrays */
@property(nonatomic,strong)NSArray *cellHeightArrays;

/** attributes arrays */
@property(nonatomic,strong)NSMutableArray *attributesArrays;

/** cell array */
@property(nonatomic,strong)NSMutableArray *tempAttributesArrays;

@end

@implementation SSCollectionViewLayout

-(instancetype)initWithArrays:(NSArray *)cellHeightArrays
{
    if (self = [super init]) {
    
        self.cellHeightArrays = cellHeightArrays;
    }

    return self;
}

//自定義layout需要重寫這些方法
-(void)prepareLayout
{
    [super prepareLayout];
    
    [self.attributesArrays  removeAllObjects];
    [self.tempAttributesArrays removeAllObjects];
    //獲取當前collectionView對應區(qū)的item
    //獲取senction全部個數(shù)
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i=0; i < count; i++) {
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow: i inSection:0]];
   
        [self.attributesArrays addObject:attributes];
    }
    
}

//返回collectionView的內(nèi)容的尺寸
-(CGSize)collectionViewContentSize
{
    CGFloat maxContentHeight = CGRectGetMaxY([[self.tempAttributesArrays firstObject] frame]);
    for (UICollectionViewLayoutAttributes *attributes in self.tempAttributesArrays) {
        if (maxContentHeight  < attributes.frame.size.height) {
            maxContentHeight = CGRectGetMaxY(attributes.frame);
        }
    }
    return CGSizeMake(self.collectionView.bounds.size.width, maxContentHeight);
}

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attributesArrays;
}
///返回對應于indexPath的位置的cell的布局屬性,返回指定indexPath的item的布局信息。子類必須重載該方法,該方法只能為cell提供布局信息,不能為補充視圖和裝飾視圖提供符匾。
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat itemWidth = (self.collectionView.bounds.size.width  - (10 + 10 + 10 * 2))/3;
    CGFloat itemHeight = [self.cellHeightArrays[indexPath.row] floatValue];
    //在這里用上cellArrays了,橫排最多顯示3個
    if (self.tempAttributesArrays.count < 3) {//設(shè)置第一排rect
        [self.tempAttributesArrays addObject:attributes];
        NSLog(@"%ld",self.tempAttributesArrays.count);
        CGRect rect  = CGRectMake(10 +( itemWidth + 10) * (self.tempAttributesArrays.count - 1), 10, itemWidth, itemHeight);
        attributes.frame = rect;
    }else{
        
        UICollectionViewLayoutAttributes *fristAttributes = [self.tempAttributesArrays firstObject];
        CGFloat minY = CGRectGetMaxY(fristAttributes.frame);
        CGFloat Y = minY;
        NSInteger index = 0;
        CGRect itemFrame = CGRectMake(fristAttributes.frame.origin.x, CGRectGetMaxY(fristAttributes.frame) + 10, itemWidth, itemHeight);
        for (UICollectionViewLayoutAttributes *attri in self.tempAttributesArrays) {
            if (minY > CGRectGetMaxY(attri.frame)) {
                minY = CGRectGetMaxY(attri.frame);
                Y = minY;
                itemFrame = CGRectMake(attri.frame.origin.x, Y + 10, itemWidth, itemHeight);
                NSInteger currentIndex = [self.tempAttributesArrays indexOfObject:attri];
                index = currentIndex;
            }
        }
       
        attributes.frame = itemFrame;
        [self.tempAttributesArrays replaceObjectAtIndex:index withObject:attributes];

    }
   
    return attributes;
}

這里我們就實現(xiàn)了一個自定義layout,例子顯示橫屏3個item瘩例,高度通過動態(tài)傳過來啊胶,實際開發(fā)中,需要跟后臺商量垛贤,返回cell高度焰坪。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市聘惦,隨后出現(xiàn)的幾起案子某饰,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件黔漂,死亡現(xiàn)場離奇詭異诫尽,居然都是意外死亡,警方通過查閱死者的電腦和手機炬守,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門牧嫉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人劳较,你說我怎么就攤上這事驹止『屏” “怎么了观蜗?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長衣洁。 經(jīng)常有香客問我墓捻,道長,這世上最難降的妖魔是什么坊夫? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任砖第,我火速辦了婚禮,結(jié)果婚禮上环凿,老公的妹妹穿的比我還像新娘梧兼。我一直安慰自己,他們只是感情好智听,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布羽杰。 她就那樣靜靜地躺著,像睡著了一般到推。 火紅的嫁衣襯著肌膚如雪考赛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天莉测,我揣著相機與錄音颜骤,去河邊找鬼。 笑死捣卤,一個胖子當著我的面吹牛忍抽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播董朝,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼梯找,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了益涧?” 一聲冷哼從身側(cè)響起锈锤,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后久免,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體浅辙,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年阎姥,在試婚紗的時候發(fā)現(xiàn)自己被綠了记舆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡呼巴,死狀恐怖泽腮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情衣赶,我是刑警寧澤诊赊,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站府瞄,受9級特大地震影響碧磅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜遵馆,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一鲸郊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧货邓,春花似錦秆撮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至复隆,卻和暖如春拨匆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背挽拂。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工惭每, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人亏栈。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓台腥,卻偏偏與公主長得像,于是被迫代替她去往敵國和親绒北。 傳聞我的和親對象是個殘疾皇子黎侈,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359