亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

UICollectionView的基礎用法

2019-11-09 18:53:13
字體:
來源:轉載
供稿:網友

UICollectionView基礎

初始化部分:

復制代碼
UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 20, 250, 350) collectionViewLayout:flowLayout];self.myCollectionView.backgroundColor = [UIColor grayColor];[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@“myCell"];self.myCollectionView.delegate = self;self.myCollectionView.dataSource = self;[self.view addSubview:self.myCollectionView];復制代碼

 

UICollectionViewLayout

UICollectionViewLayout決定了UICollectionView如何顯示在界面上,Apple提供了一個最簡單的默認layout對象:UICollectionViewFlowLayout。

Flow Layout是一個Cells的線性布局方案,并具有頁面和頁腳。其可定制的內容如下:

itemSize屬性

設定全局的Cell尺寸,如果想要單獨定義某個Cell的尺寸,可以使用下面方法:

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

minimumLineSpacing屬性

設定全局的行間距,如果想要設定指定區內Cell的最小行距,可以使用下面方法:

(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

minimumInteritemSpacing屬性

設定全局的Cell間距,如果想要設定指定區內Cell的最小間距,可以使用下面方法:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

scrollDirection屬性

設定滾動方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal兩個值。

headerReferenceSize屬性與footerReferenceSize屬性

設定頁眉和頁腳的全局尺寸,需要注意的是,根據滾動方向不同,header和footer的width和height中只有一個會起作用。如果要單獨設置指定區內的頁面和頁腳尺寸,可以使用下面方法:

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

sectionInset屬性

設定全局的區內邊距,如果想要設定指定區的內邊距,可以使用下面方法:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;


 

然后需要實現三種類型的委托:UICollectionViewDataSource, UICollectionViewDelagate和UICollectionViewDelegateFlowLayout。

@interface ViewController : UIViewController 

因為UICollectionViewDelegateFlowLayout實際上是UICollectionViewDelegate的一個子協議,它繼承了UICollectionViewDelegate,所以只需要在聲明處寫上UICollectionViewDelegateFlowLayout就行了。


 

UICollectionViewDataSource

(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

返回collection view里區(section)的個數,如果沒有實現該方法,將默認返回1:

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

 

(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

返回指定區(section)包含的數據源條目數(number of items),該方法必須實現:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 7;}

 

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

返回某個indexPath對應的cell,該方法必須實現:

復制代碼
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];    if(indexPath.section==0)    {        cell.backgroundColor = [UIColor redColor];    }    else if(indexPath.section==1)    {        cell.backgroundColor = [UIColor greenColor];    }    return cell;}復制代碼

UICollectionViewCell結構上相對比較簡單,由下至上:

首先是cell本身作為容器view然后是一個大小自動適應整個cell的backgroundView,用作cell平時的背景再其次是selectedBackgroundView,是cell被選中時的背景最后是一個contentView,自定義內容應被加在這個view上

 

(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath *)indexPath

為collection view添加一個補充視圖(頁眉或頁腳)

 

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

設定頁眉的尺寸

 

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

設定頁腳的尺寸

 

(void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString*)identifier

添加頁眉和頁腳以前需要注冊類和標識:


 

添加補充視圖的代碼示例:

復制代碼
[self.myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader"];[self.myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader"];-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{    CGSize size = {240,25};    return size;}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{    CGSize size = {240,25};    return size;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    MyHeadView *headView;        if([kind isEqual:UICollectionElementKindSectionHeader])    {         headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];        [headView setLabelText:[NSString stringWithFormat:@"section %d's header",indexPath.section]];    }    else if([kind isEqual:UICollectionElementKindSectionFooter])    {        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];        [headView setLabelText:[NSString stringWithFormat:@"section %d's footer",indexPath.section]];    }    return headView;}復制代碼

 

MyHeadView.h

#import @interface MyHeadView : UICollectionReusableView- (void) setLabelText:(NSString *)text;@end

 

MyHeadView.m

復制代碼
#import "MyHeadView.h"@interface MyHeadView()@PRoperty (strong, nonatomic) UILabel *label;@end@implementation MyHeadView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self)    {        self.label = [[UILabel alloc] init];        self.label.font = [UIFont systemFontOfSize:18];        [self addSubview:self.label];    }    return self;}- (void) setLabelText:(NSString *)text{    self.label.text = text;    [self.label sizeToFit];}@end復制代碼

 

在注冊Cell和補充視圖時,也可以用新建xib文件的方式:

復制代碼
[self.myCollectionView registerNib:[UINib nibWithNibName:@"MyCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"hxwCell"];[self.myCollectionView registerNib:[UINib nibWithNibName:@"MySupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader"];    [self.myCollectionView registerNib:[UINib nibWithNibName:@"MySupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwFooter"];復制代碼

用這種方式注冊后,甚至可以不用新建類去綁定這個xib,直接通過viewWithTag的方式獲取xib里的控件:

UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];UILabel *label = (UILabel *)[view viewWithTag:1];label.text = @"empty";

 


 

UICollectionViewDelegateFlowLayout

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

設定指定Cell的尺寸

復制代碼
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    if(indexPath.section==0 && indexPath.row==1)    {        return CGSizeMake(50, 50);    }    else    {        return CGSizeMake(75, 30);    }}復制代碼

 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

設定collectionView(指定區)的邊距

復制代碼
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{    if(section==0)    {        return UIEdgeInsetsMake(35, 25, 15, 25);    }    else    {        return UIEdgeInsetsMake(15, 15, 15, 15);    }}復制代碼

 

(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

設定指定區內Cell的最小行距,也可以直接設置UICollectionViewFlowLayout的minimumLineSpacing屬性

復制代碼
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{    if(section==0)    {        return 10.0;    }    else    {        return 20.0;    }}復制代碼

 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

設定指定區內Cell的最小間距,也可以直接設置UICollectionViewFlowLayout的minimumInteritemSpacing屬性

復制代碼
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{    if(section==0)    {        return 10.0;    }    else    {        return 20.0;    }}復制代碼

 


UICollectionViewDelegate

(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

當指定indexPath處的item被選擇時觸發

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{[self.myArray removeObjectAtIndex:indexPath.row];[collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];}

P.s. 當你刪除或添加元素時,一定要更新numberOfItemsInSection的返回情況。

 

(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

當指定indexPath處的item被取消選擇時觸發,僅在允許多選時被調用

 

下面是三個和高亮有關的方法:

(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath

(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath

 

事件的處理順序如下:

手指按下shouldHighlightItemAtIndexPath (如果返回YES則向下執行,否則執行到這里為止)didHighlightItemAtIndexPath (高亮)手指松開didUnhighlightItemAtIndexPath (取消高亮)shouldSelectItemAtIndexPath (如果返回YES則向下執行,否則執行到這里為止)didSelectItemAtIndexPath (執行選擇事件)

如果只是簡單實現點擊后cell改變顯示狀態,只需要在cellForItemAtIndexPath方法里返回cell時,指定cell的selectedBackgroundView:

復制代碼
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];        UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];    selectedBGView.backgroundColor = [UIColor blueColor];    cell.selectedBackgroundView = selectedBGView;        return cell;}復制代碼

如果要實現點擊時(手指未松開)的顯示狀態與點擊后(手指松開)的顯示狀態,則需要通過上面提到的方法來實現:

復制代碼
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];        [cell setBackgroundColor:[UIColor purpleColor]];}- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];        [cell setBackgroundColor:[UIColor yellowColor]];}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
xvideos成人免费中文版| 亚洲一区二区三区在线免费观看| 国产精品电影网站| 亚洲全黄一级网站| 91免费国产网站| 日本久久久久久久久| 亚洲成年网站在线观看| 狠狠躁夜夜躁人人爽天天天天97| 日韩有码在线电影| 日韩美女视频免费在线观看| 国产+成+人+亚洲欧洲| 高跟丝袜欧美一区| 亚洲伊人久久大香线蕉av| 欧美激情视频网| 国产精品视频999| 中文字幕亚洲无线码a| 国内精久久久久久久久久人| 国产亚洲欧洲高清一区| 青草青草久热精品视频在线网站| 欧美第一淫aaasss性| 国产精品香蕉在线观看| 成人黄色片网站| 超碰精品一区二区三区乱码| 国产精品99久久久久久人| 中文字幕亚洲一区在线观看| 91久久久久久久久| 日韩理论片久久| 欧美性20hd另类| 亚洲视频综合网| 日本精品一区二区三区在线播放视频| 日韩av综合网| 永久免费看mv网站入口亚洲| 精品中文视频在线| 日韩在线观看免费网站| 日本免费一区二区三区视频观看| 国产精品久久久久久久久久久久久久| 久久久极品av| 麻豆国产va免费精品高清在线| 91亚洲精品在线观看| 欧美做受高潮1| 日韩视频免费看| 欧美成人久久久| 日韩欧美精品网址| 欧美性xxxxx| 欧美精品激情在线观看| 亚洲伊人一本大道中文字幕| 欧美电影在线免费观看网站| 一本一本久久a久久精品综合小说| 亚洲第一区中文99精品| 亚洲a级在线播放观看| 日韩免费av在线| 亚洲国产一区二区三区在线观看| 日韩在线视频网| 日韩av综合中文字幕| 欧美一区二三区| 欧美国产精品va在线观看| 国产精品亚洲片夜色在线| 亚洲系列中文字幕| 成人激情视频在线观看| 91chinesevideo永久地址| 午夜精品99久久免费| 日韩在线视频国产| 日韩一区二区福利| 国产精品电影网| 欧美疯狂做受xxxx高潮| 成人国产精品久久久久久亚洲| 欧美性猛交xxxx免费看漫画| 成人黄色av播放免费| 中国china体内裑精亚洲片| 95av在线视频| 色琪琪综合男人的天堂aⅴ视频| 有码中文亚洲精品| 欧美一级在线亚洲天堂| 久久亚洲国产精品成人av秋霞| 日韩欧美成人网| 欧美精品免费看| 国产欧美一区二区三区久久| 色中色综合影院手机版在线观看| 7777免费精品视频| 亚洲精品视频网上网址在线观看| 久久久久久久电影一区| 日本电影亚洲天堂| 亚洲二区在线播放视频| 精品自在线视频| 国产欧美 在线欧美| 狠狠躁18三区二区一区| 亚洲欧美制服中文字幕| 欧美激情在线播放| 久久亚洲电影天堂| 黄色91在线观看| 91高潮在线观看| 日韩视频亚洲视频| 国精产品一区一区三区有限在线| 精品久久久久久久中文字幕| 国产精品最新在线观看| 欧美日韩国产成人在线观看| 中文字幕欧美日韩精品| 日韩精品视频在线观看网址| 国产精品吴梦梦| 国产精品视频久久| 日韩免费不卡av| 欧美激情久久久久久| 日韩精品视频观看| 国产99久久精品一区二区| 日韩综合视频在线观看| 欧美激情综合色综合啪啪五月| 亚洲福利视频二区| 亚洲级视频在线观看免费1级| 国产亚洲精品va在线观看| 精品久久久久久久久久国产| 亚洲第一视频网| 国产精品草莓在线免费观看| 免费99精品国产自在在线| 欧美xxxx综合视频| 久久精品国产69国产精品亚洲| 亚洲精品电影网| 日韩精品在线免费播放| 中文字幕在线视频日韩| 亚洲男人天堂手机在线| 成人激情视频免费在线| 国内精品久久久久久中文字幕| 日韩大片免费观看视频播放| 欧美性猛交xxxx富婆| 91久久久久久久一区二区| 成人免费高清完整版在线观看| 欧美激情第一页xxx| 91中文在线视频| 色久欧美在线视频观看| 精品一区二区电影| 亚洲free性xxxx护士hd| 亚洲欧美日本精品| 欧美激情视频在线免费观看 欧美视频免费一| 国产亚洲视频中文字幕视频| 日韩中文字幕欧美| 97国产在线观看| 九九热这里只有精品6| 国产精品欧美在线| 疯狂做受xxxx欧美肥白少妇| 在线观看久久久久久| 成人国产精品av| 91精品国产91久久久久| 亚洲国产精品女人久久久| 成人免费视频网址| 久久香蕉国产线看观看av| 亚洲女成人图区| 国产精品白嫩美女在线观看| 欧美国产一区二区三区| 亚洲欧美一区二区三区四区| 欧美亚洲在线观看| 国产精品美女av| 日韩精品福利网站| 精品一区二区三区电影| 成人久久精品视频| 自拍偷拍亚洲一区| 1769国内精品视频在线播放| 亚洲网站在线观看| 欧洲美女7788成人免费视频| 国产日韩精品在线播放| 97色在线视频| 日韩在线视频导航| 亚洲а∨天堂久久精品9966| 日韩高清av一区二区三区| 国产日韩在线一区| 久久亚洲综合国产精品99麻豆精品福利|