如何 布局一個(gè)collectionView瀑布流

ios UICollectionView實(shí)現(xiàn)瀑布流?

通過(guò)自定義collectionViewCell和重寫collectionViewLayout

一.自定義UICollectionViewCell

如>#import@interfaceCollectionCell : UICollectionViewCell

@property (strong, nonatomic)? UIImageView*imageView;

@property (strong, nonatomic)? UIImageView*bottomBar;

@property (strong, nonatomic) CBAutoScrollLabel*productNameLbl;

@property (strong, nonatomic) UILabel*priceLbl;@end////CollectionCell.m//CollectionView////Created by Piosa on 14-6-13.//Copyright (c) 2014年 D2space. All rights reserved.//#import"CollectionCell.h"@implementationCollectionCell- (id)initWithFrame:(CGRect)frame{self=[super initWithFrame:frame];if(self){self.imageView=[[UIImageView alloc]init];[selfaddSubview:self.imageView];//--------------//透明欄//--------------floath=30;floatx=0;floatw=CGRectGetWidth(frame);floaty=0;self.bottomBar=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];[selfaddSubview:self.bottomBar];self.bottomBar.image=[UIImage imageNamed:@"toumingse.png"];//產(chǎn)品名y=0;floattempH=h/2;x=3;self.productNameLbl=[[CBAutoScrollLabel alloc]initWithFrame:CGRectMake(x, y, w, tempH)];self.productNameLbl.backgroundColor=[UIColor clearColor];[self.bottomBar addSubview:self.productNameLbl];//產(chǎn)品價(jià)格y+=tempH;self.priceLbl=[[UILabel alloc]initWithFrame:CGRectMake(x, y, w, tempH)];self.priceLbl.textColor=[UIColor whiteColor];self.priceLbl.backgroundColor=[UIColor clearColor];self. priceLbl.font=[UIFont systemFontOfSize:12];[self.bottomBar addSubview:self.priceLbl];}returnself;}@end二.創(chuàng)建自定義布局#import#pragmamark WaterF@protocolWaterFLayoutDelegate @required- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

@optional- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout heightForHeaderInSection:(NSInteger)section;- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout heightForFooterInSection:(NSInteger)section;@end@interfaceMyLayout : UICollectionViewLayout{floatx;floatleftY;floatrightY;

}

@propertyfloatitemWidth;@property (nonatomic, assign) CGPoint center;@property (nonatomic, assign) CGFloat radius;@property (nonatomic, assign) NSInteger cellCount;///The delegate will point to collection view's delegate automatically.@property (nonatomic, weak)iddelegate;///Array to store attributes for all items includes headers, cells, and footers@property (nonatomic, strong) NSMutableArray*allItemAttributes;@property (nonatomic, assign) UIEdgeInsets sectionInset;@end#import"MyLayout.h"#defineITEM_SIZE 70@implementationMyLayout-(void)prepareLayout{[super prepareLayout];self.itemWidth=150;self.sectionInset=UIEdgeInsetsMake(5,5,5,5);self.delegate= (id )self.collectionView.delegate;CGSize size=self.collectionView.frame.size;_cellCount= [[selfcollectionView] numberOfItemsInSection:0];_center= CGPointMake(size.width /2.0, size.height /2.0);_radius= MIN(size.width, size.height) /2.5;

}-(CGSize)collectionViewContentSize{returnCGSizeMake(320, (leftY>rightY?leftY:rightY));

}- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath? withIndex:(int)index{CGSize itemSize= [self.delegatecollectionView:self.collectionView layout:selfsizeForItemAtIndexPath:indexPath];CGFloat itemHeight= floorf(itemSize.height *self.itemWidth /itemSize.width);UICollectionViewLayoutAttributes*attributes =[UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];index+=1;if(index%2==0){x+=(self.itemWidth+self.sectionInset.left);rightY+=self.sectionInset.top;attributes.frame=CGRectMake(x, rightY,self.itemWidth, itemHeight);rightY+=itemHeight;}else{x=self.sectionInset.left;leftY+=self.sectionInset.top;attributes.frame=CGRectMake(x, leftY,self.itemWidth, itemHeight);leftY+=itemHeight;}returnattributes;

}-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{x=0;leftY=0;rightY=0;NSMutableArray* attributes =[NSMutableArrayarray];NSLog(@"cellCount=%d",self.cellCount);for(NSInteger i=0; i

}- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath*)itemIndexPath{UICollectionViewLayoutAttributes* attributes =[selflayoutAttributesForItemAtIndexPath:itemIndexPath];attributes.alpha=0.0;attributes.center=CGPointMake(_center.x, _center.y);returnattributes;

}- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath*)itemIndexPath{UICollectionViewLayoutAttributes* attributes =[selflayoutAttributesForItemAtIndexPath:itemIndexPath];attributes.alpha=0.0;attributes.center=CGPointMake(_center.x, _center.y);attributes.transform3D= CATransform3DMakeScale(0.1,0.1,1.0);returnattributes;

}@end三.創(chuàng)建UICollectionView用之前自定義的布局進(jìn)行初始化并注冊(cè)之前自定義的UICollectionViewCell晨抡,參照如下1.創(chuàng)建變量

@property (strong, nonatomic)? UICollectionView*collectionView;2.初始化

MyLayout*layout=[[MyLayout alloc]init];collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0,0, CGRectGetWidth(self.frame),CGRectGetHeight(self.frame)) collectionViewLayout:layout];collectionView.delegate=self;collectionView.dataSource=self;collectionView.scrollEnabled=YES;[selfaddSubview:collectionView];collectionView.backgroundColor=[UIColor clearColor];[collectionView registerClass:[CollectionCellclass] forCellWithReuseIdentifier:@"CollectionCell"];3.實(shí)現(xiàn)代理#pragma-mark UICollectionView delegate//根據(jù)傳入的圖片得到寬高-(CGSize)getImgSize:(UIImage *)image{//得到比例floatrate=(itemWidth/image.size.width);returnCGSizeMake(itemWidth, (image.size.height*rate));

}//定義每個(gè)UICollectionView 的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{NSDictionary*item=productList[indexPath.row];return[selfgetImgSize:[item objectForKey:KEY_PRODUCT_IMG]];}//定義每個(gè)UICollectionView 的 margin-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{returnUIEdgeInsetsMake(5,5,5,5);

}-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"indexPath=%d",indexPath.row);NSDictionary*item=productList[indexPath.row];DetailViewController*detailView=[[DetailViewController alloc]init];detailView.productID=[[item objectForKey:PRODUCT_ID] integerValue];[viewController.navigationController pushViewController:detailView animated:YES]; }//每個(gè)section的item個(gè)數(shù)-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{returnproductList.count;

}-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViews cellForItemAtIndexPath:(NSIndexPath*)indexPath{CollectionCell*cell = (CollectionCell *)[collectionViewsdequeueReusableCellWithReuseIdentifier:@"CollectionCell"forIndexPath:indexPath];cell.backgroundColor=[UIColor clearColor];//圖片名稱//NSString *imageToLoad = [NSString stringWithFormat:@"%d.png", indexPath.row];NSDictionary*item=productList[indexPath.row];NSString*productName;NSString*productImgUrl;if([dataTypeps isEqualToString:TYPE_HISTORY]){NSArray*temp=[[item objectForKey:PRODUCT_NAME] componentsSeparatedByString:@":"];productName=temp[0];}else{productName=[item objectForKey:PRODUCT_NAME];}UIImage*img=[item objectForKey:KEY_PRODUCT_IMG];CGSize size=[selfgetImgSize:img];//加載圖片cell.imageView.image=img;cell.imageView.frame=CGRectMake(0,0, size.width, size.height);//--------------//透明欄//--------------floath=30;floatx=0;floatw=size.width;floaty=size.height-h;cell.bottomBar.frame=CGRectMake(x, y, w, h);cell.bottomBar.backgroundColor=[UIColor clearColor];//產(chǎn)品名y=0;floattempH=h/2;x=3;cell.productNameLbl.frame=CGRectMake(x, y, w, tempH);cell.productNameLbl.backgroundColor=[UIColor clearColor];[commonUtil setScrollLabel:cell.productNameLbl withText:productName withCenter:UITextAlignmentLeftwithFontSize:14withTextColor:[UIColor whiteColor]];//產(chǎn)品價(jià)格y+=tempH;cell.priceLbl.frame=CGRectMake(x, y, w, tempH);cell.priceLbl.text=[NSString stringWithFormat:@"¥%@",[item objectForKey:PRODUCT_PRICE]];returncell;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子凉敲,更是在濱河造成了極大的恐慌,老刑警劉巖芝囤,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡搓译,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門锋喜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)些己,“玉大人,你說(shuō)我怎么就攤上這事嘿般《伪辏” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵博个,是天一觀的道長(zhǎng)怀樟。 經(jīng)常有香客問(wèn)我功偿,道長(zhǎng)盆佣,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任械荷,我火速辦了婚禮共耍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吨瞎。我一直安慰自己痹兜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布颤诀。 她就那樣靜靜地躺著字旭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪崖叫。 梳的紋絲不亂的頭發(fā)上遗淳,一...
    開(kāi)封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音心傀,去河邊找鬼屈暗。 笑死,一個(gè)胖子當(dāng)著我的面吹牛脂男,可吹牛的內(nèi)容都是我干的养叛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼宰翅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼弃甥!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起汁讼,我...
    開(kāi)封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤淆攻,失蹤者是張志新(化名)和其女友劉穎肮之,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體卜录,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡戈擒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了艰毒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筐高。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖丑瞧,靈堂內(nèi)的尸體忽然破棺而出柑土,到底是詐尸還是另有隱情,我是刑警寧澤绊汹,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布稽屏,位于F島的核電站,受9級(jí)特大地震影響西乖,放射性物質(zhì)發(fā)生泄漏狐榔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一获雕、第九天 我趴在偏房一處隱蔽的房頂上張望薄腻。 院中可真熱鬧,春花似錦届案、人聲如沸庵楷。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)尽纽。三九已至,卻和暖如春童漩,著一層夾襖步出監(jiān)牢的瞬間弄贿,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工睁冬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挎春,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓豆拨,卻偏偏與公主長(zhǎng)得像直奋,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子施禾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容