iOS:UICollectionView的構建及使用,

來源:互聯網
上載者:User

iOS:UICollectionView的構建及使用,
第一部分,三個協議方法,先介紹兩個

<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout > 

前兩個的用法和tableView的很像,第三個是頭視圖尾視圖的協議。(頭視圖尾視圖,一樣要寫代理,寫註冊,缺少了就不行。)註冊以後,就不需要再去管理複用的問題了。這點就很簡單。這個如果用好的話,會非常的簡單。很多事情迎刃而解,否則使用tableView的話,需要三個tableView一起滑動,彼此之間需要觀察,一旦變化隨之變化,用scroller 的ContentOffset 來控制滑動,非常不科學,用這個的話三個就一起滑動了。
第二部分,構建先得建立一個layout,UICollectionViewFlowLayout  這個類型的
//建立布局對象UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];//設定儲存格的尺寸flowLayout.itemSize = CGSizeMake(80, 80);//設定頭視圖高度flowLayout.headerReferenceSize = CGSizeMake(0, 30);//flowlaout的屬性,橫向滑動flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//接下來就在建立collectionView的時候初始化,就很方便了(能直接帶上layout)_myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, 320, 280) collectionViewLayout:flowLayout];_myCollectionView.tag = 200;_myCollectionView.backgroundColor = [UIColor greenColor]; _myCollectionView.delegate = self;_myCollectionView.dataSource = self;//添加到首頁面上去 [self.view addSubview:_myCollectionView];//collectionCell的註冊[_myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myheheIdentifier"];//collection頭視圖的註冊   奇葩的地方來了,頭視圖也得註冊[_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Identifierhead”];#pragma mark -UICollectionViewDataSource//指定組的個數 ,一個大組!!不是一排,是N多排組成的一個大組(與下面區分)- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return 2;}//指定儲存格的個數 ,這個是一個組裡面有多少儲存格,e.g : 一個儲存格就是一張圖片- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 10;}//構建儲存格- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    if (collectionView.tag == 200) {        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myheheIdentifier" forIndexPath:indexPath];        cell.backgroundColor = [UIColor purpleColor];        return cell;    }}//組的頭視圖建立- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Identifierhead" forIndexPath:indexPath];    headView.backgroundColor = [UIColor blueColor];    return headView;}//通過協議方法設定儲存格尺寸- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    CGFloat rd = arc4random()%90;    return CGSizeMake(90, rd);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.