標籤:
一.集合視圖(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).