iOS學習之UICollectionView使用註解

來源:互聯網
上載者:User

標籤:

# import "ViewC.h" # import "CLCollectionViewCell.h" # import "HeadView.h" # import "FootView.h" static NSString *cellIdentifier = @ "cell" ; static NSString *headerIdentifier = @ "header" ; static NSString *footerIdentifier = @ "footer" ; @interface ViewC ()<uicollectionviewdatasource, uicollectionviewdelegate,= "" uicollectionviewdelegateflowlayout= "" >  @end  @implementation ViewC   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {      self = [ super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];      if (self) {          // Custom initialization      }      return self; }  - ( void )viewDidLoad {      [ super viewDidLoad];      // Do any additional setup after loading the view.      self.view.backgroundColor = [UIColor lightGrayColor];      UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];      flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; //滾動方向      flowLayout.minimumLineSpacing = 10.0 ; //行間距(最小值)      flowLayout.minimumInteritemSpacing = 50.0 ; //item間距(最小值)      flowLayout.itemSize = CGSizeMake( 50 , 50 ); //item的大小      flowLayout.sectionInset = UIEdgeInsetsMake( 10 , 10 , 10 , 10 ); //設定section的邊距      flowLayout.headerReferenceSize = CGSizeMake( 320 , 20 );      flowLayout.footerReferenceSize = CGSizeMake( 320 , 20 );      //第二個參數是cell的布局      UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake( 0 , 0 , 320 , 568 ) collectionViewLayout:flowLayout];      [flowLayout release];      collectionView.dataSource = self;      collectionView.delegate = self;      collectionView.backgroundColor = [UIColor orangeColor];      //1 註冊複用cell(cell的類型和標識符)(可以註冊多個複用cell, 一定要保證重用標示符是不一樣的)註冊到了collectionView的複用池裡      [collectionView registerClass:[CLCollectionViewCell class ] forCellWithReuseIdentifier:cellIdentifier];            //第一個參數:返回的View類型      //第二個參數:設定View的種類(header, footer)      //第三個參數:設定重用標識符      [collectionView registerClass:[HeadView class ] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];      [collectionView registerClass:[FootView class ] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];      [self.view addSubview:collectionView];      [collectionView release]; }  - ( void )didReceiveMemoryWarning {      [ super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated. }  #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {      return 10 ; }  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {      return 10 ; }  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {      //2 從複用池中找cell(1:cell的標示符 2:indexPath決定系統用不用再給你建立cell, 不用建立的話, 就直接使用之前的cell)      CLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];      cell.backgroundColor = [UIColor yellowColor];      cell.textLabel.text = [NSString stringWithFormat:@ "%ld" , ( long )indexPath.row];      return cell; }  - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {      if (kind == UICollectionElementKindSectionHeader) {          HeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];          headerView.textLabel.text = @ "讓我組成頭部!" ;          headerView.textLabel.textAlignment = NSTextAlignmentCenter;          headerView.textLabel.textColor = [UIColor whiteColor];          return headerView;      }      FootView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];      footerView.textLabel.text = @ "讓我組成尾部!" ;      footerView.textLabel.textAlignment = NSTextAlignmentCenter;      footerView.textLabel.textColor = [UIColor whiteColor];      return footerView; }  #pragma mark - UICollectionViewDelegate - ( void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {      NSLog(@ "%d %d" , indexPath.section, indexPath.row); }  #pragma mark - UICollectionViewDelegateFlowLayout /* - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {       } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {       } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {       } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {       } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {       }   */ /* #pragma mark - Navigation  // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {      // Get the new view controller using [segue destinationViewController].      // Pass the selected object to the new view controller. } */  @end

iOS學習之UICollectionView使用註解

聯繫我們

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