UICollectionView知道這些就夠用了

UICollectionView的使用

1.要點(diǎn)說(shuō)明

  •  1.三個(gè)代理前兩個(gè)的用法和tableView的很像放钦,第三個(gè)是布局的協(xié)議最筒。(注意:頭視圖尾視圖都是由代理方法獲得,而且需要寫注冊(cè)辙培,缺少了也不行邢锯。) 注冊(cè)以后丹擎,就不需要再去管理復(fù)用的問題了。這點(diǎn)就很簡(jiǎn)單再愈。這個(gè)如果用好的話护戳,會(huì)非常的簡(jiǎn)單媳荒。
    
  • 2.item(即cell)的布局原理(類似tableView)
  • 3.header/footer View的布局原理都是繼承于UICollectionReusableView,并且必須要從dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:這個(gè)代理方法中獲取缴渊,而且根據(jù)不同的kind作為headerView或者footerView衔沼,切記:collectionView中頭部視圖和尾部視圖也需要注冊(cè)

2.代碼

/******************************** UIViewController.h  **************************/
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{

}
@property (strong, nonatomic)UICollectionView *collectionView;

@end

/******************************** UIViewController.m  **************************/

#import "UIViewController.h"
#import "UICollectionHeaderView.h"
#import "UICollectionFooterView.h"
#import "UICollectionViewCell.h"

@interface UIViewController ()

@end

//cell俐巴、header、footerView 標(biāo)識(shí)
static NSString *cellIdentifier = @"UICollectionViewCell";
static NSString *headerIdentifier = @"UICollectionHeaderView";
static NSString *footerIdentifier = @"UICollectionFooterView";

@implementation UIViewController

- (void)viewDidLoad
{
      [super viewDidLoad];
     //創(chuàng)建布局對(duì)象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //flowlaout的屬性缘圈,確定是水平滾動(dòng)袜蚕,還是垂直滾動(dòng)
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    //接下來(lái)就在創(chuàng)建collectionView的時(shí)候初始化,帶上布局方法.
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, 320, 280) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor]; 
   //添加View 上面
    [self.view addSubview:self.collectionView];
   
   //指定數(shù)據(jù)源和代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

   

**注冊(cè)方法分2種:
  1.代碼創(chuàng)建的方式
  2.xib 方式創(chuàng)建**


    //collectionCell的注冊(cè)
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];

    //collection頭視圖的注冊(cè)遣疯。  
**注意:和TableView不同缠犀,頭視圖和尾部視圖也得注冊(cè)**
    [self.collectionView registerClass:[UICollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
    [self.collectionView registerClass:[UICollectionFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:footerIdentifier];

      /*另一種方式,從Xib上注冊(cè)
    UINib *nib = nil;
    nib = [UINib nibWithNibName:cellIdentifier bundle:nil];
    [self.collectionView registerNib:nib
                 forCellWithReuseIdentifier:cellIdentifier];
    //注冊(cè)headerview和footerview
    nib = [UINib nibWithNibName:headerIdentifier bundle:nil];
    [self.collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
    nib = [UINib nibWithNibName:footerIdentifier bundle:nil];
    [self.collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
    
 
}

**遵守的協(xié)議:**
 **UICollectionViewDataSource/Delegate/DelegateFlowLayout**

#pragma mark  UICollectionViewDataSource/Delegate

//1.返回多少組(section),此方法不寫默認(rèn)是1組(跟tableview類似)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

#pragma mark required
//2.每組返回多少個(gè)Item滔迈,這個(gè)Item類似tableview中的cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

//3.返回cell被辑,這里構(gòu)建Item(即cell)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //這里是自定義的cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

#pragma mark  optional
//4.自定義header/footerView
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
//這里返回的view必須要從dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:這個(gè)方法中獲取盼理,而且根據(jù)不同的kind作為headerView或者footerView榜揖,切記:collectionView中頭部視圖和尾部視圖也需要注冊(cè)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if (UICollectionElementKindSectionHeader == kind) {
        //這里是自定義的頭視圖
        UICollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor whiteColor]; 
        return headerView;
    }
    if (UICollectionElementKindSectionFooter == kind {
        //這里是自定義的尾視圖               
        UICollectionFooterView *footerView = collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor whiteColor]; 
        return footerView;
    }
    return reusableView;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//5.布局確定每個(gè)Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    return CGSizeMake(100, 100);
}

//6.布局確定每個(gè)section內(nèi)的Item距離section四周的間距 UIEdgeInsets
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//7.返回每個(gè)section內(nèi)上下兩個(gè)Item之間的間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10; 
}
//8.返回每個(gè)section內(nèi)左右兩個(gè)Item之間的間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

//9.返回headerView的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat width = CGRectGetWidth(collectionView.bounds);
    CGFloat height = width + 86;
    return CGSizeMake(width, height);
}

//10.返回footerView的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    CGFloat width = CGRectGetWidth(collectionView.bounds);
    CGFloat height = 144;
    return CGSizeMake(width, height);
}

#pragma mark --UICollectionViewDelegate

//11.UICollectionView被選中時(shí)調(diào)用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    NSLog(@"item======%ld",(long)indexPath.item);
    NSLog(@"row=======%ld",(long)indexPath.row);
    NSLog(@"section===%ld",(long)indexPath.section);
}

//返回這個(gè)UICollectionView是否可以被選擇
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

希望可以幫助到大家.......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迅矛,一起剝皮案震驚了整個(gè)濱河市秽褒,隨后出現(xiàn)的幾起案子威兜,更是在濱河造成了極大的恐慌椒舵,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件犁钟,死亡現(xiàn)場(chǎng)離奇詭異涝动,居然都是意外死亡炬灭,警方通過(guò)查閱死者的電腦和手機(jī)醋粟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)重归,“玉大人昔穴,你說(shuō)我怎么就攤上這事√崆埃” “怎么了吗货?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)狈网。 經(jīng)常有香客問我宙搬,道長(zhǎng),這世上最難降的妖魔是什么拓哺? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮士鸥,結(jié)果婚禮上闲孤,老公的妹妹穿的比我還像新娘。我一直安慰自己烤礁,他們只是感情好讼积,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布肥照。 她就那樣靜靜地躺著,像睡著了一般勤众。 火紅的嫁衣襯著肌膚如雪舆绎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天们颜,我揣著相機(jī)與錄音吕朵,去河邊找鬼。 笑死窥突,一個(gè)胖子當(dāng)著我的面吹牛努溃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播阻问,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼梧税,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了则拷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤曹鸠,失蹤者是張志新(化名)和其女友劉穎煌茬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體彻桃,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坛善,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了邻眷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片眠屎。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖肆饶,靈堂內(nèi)的尸體忽然破棺而出改衩,到底是詐尸還是另有隱情,我是刑警寧澤驯镊,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布葫督,位于F島的核電站,受9級(jí)特大地震影響板惑,放射性物質(zhì)發(fā)生泄漏橄镜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一冯乘、第九天 我趴在偏房一處隱蔽的房頂上張望洽胶。 院中可真熱鬧,春花似錦裆馒、人聲如沸姊氓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)他膳。三九已至响逢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間棕孙,已是汗流浹背舔亭。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蟀俊,地道東北人钦铺。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像肢预,于是被迫代替她去往敵國(guó)和親矛洞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

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