第二十一講.UICollectionView(集合視圖)以及瀑布流效果, 通知中樞(NSNotificationCenter).

來源:互聯網
上載者:User

標籤:

一.集合視圖(UICollectionView)

1.集合視圖的概念

 

 

2.如何建立

 

3.集合視圖的布局UICollectionViewFlowLayout

 

 

 

4.自訂cell和 布局協議UICollectionViewDelegateFlowLayout

     使用時cell一般選擇自訂,而在布局時候要使用代理.

 

 UICollectionView的基本使用範例程式碼分析:

 

#import "ViewController.h"#import "CollectionReusableView.h"@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@property(nonatomic,strong)UICollectionView *collectionView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        [self loadCollectionView];    //註冊方法建立UICollectionView    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];     }-(void)loadCollectionView{    //布局類 控制每一個item的樣式    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];    flowLayout.itemSize = CGSizeMake(100, 100);        //改變上下行的大小(預設是10)    flowLayout.minimumLineSpacing = 20;        //設定兩個item中間的大小(預設為10)    flowLayout.minimumInteritemSpacing = 37.5;    //控制舉例螢幕的距離  上 左 下 右    //flowLayout.sectionInset = UIEdgeInsetsMake(100, 100, 100, 0);            //橫向滑動    //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;            self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];    //背景色    self.collectionView.backgroundColor = [UIColor whiteColor];    self.collectionView.delegate = self;    self.collectionView.dataSource = self;    [self.view addSubview:self.collectionView];        //註冊haeder和footer方法     //給collectionView 註冊一個headerView    [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];    //給collectionView 註冊一個footerView      [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];        }//返回header和footer  設定header和footer-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];                return header;    }  else{            UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];                return footer;            }   }//設定collectionView的Footer和header的大小-------UICollectionViewDelegateFlowLayout代理方法----------//設定collectionView的Footer的大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{    return CGSizeMake(100, 100);}//設定collectionView的header大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(100, 100);}//建立UICollectionView時,需要實現的兩個代理方法------------------------------------(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 100;}-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    //UICollectionViewCell 沒有本身攜帶的控制項,就是一個空白的    //所以在項目中一般都自訂一個UICollectionViewCell    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];    //設定背景顏色    cell.backgroundColor = [UIColor redColor];        return cell;}

 


 

 

二.通知中樞(NSNotificationCenter)

 

第二十一講.UICollectionView(集合視圖)以及瀑布流效果, 通知中樞(NSNotificationCenter).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.