iOS UIcollectionViewの使用方法の詳細

4935 ワード

元ブログサイト:https://blog.csdn.net/mazegong/article/details/51247878メッシュ(Mesh)

import "RootViewController.h"


import "CustomCollectionViewCell.h"


@interface RootViewController ()
@property (nonatomic,retain)NSDictionary *itemDic;//itemに表示されている画像と文字@property(nonatomic,retain)NSMutableArray*allDataArray;成方のすべてのitemに表示されている内容は、実は小さな辞書@endを盛ることです
@implementation RootViewController
  • (NSMutableArray *)allDataArray{ if (!_allDataArray) { _allDataArray = [[NSMutableArray alloc]init]; }return _allDataArray; }
  • (void)viewDidLoad { [super viewDidLoad]; for (int i = 1; i < 20; i++) {
      NSDictionary *itemDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 [NSString stringWithFormat:@"%d.jpg",i],@"imageName",
                                 [NSString stringWithFormat:@" %d ",i] ,@"textLable",
                                 nil];
      [self.allDataArray addObject:itemDic];
    
    } self.navigationItem.title = @"github";//コレクションビューを初期化するには、レイアウトオブジェクトを初期化する必要があるため、レイアウトオブジェクトを初期化し、そのプロパティ//これを設定する必要があります.これはシステムが提供するレイアウトクラスで、比較ルールのレイアウトをレイアウトすることができます.UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];//各itemのサイズを設定するitemSize = CGSizeMake(120, 160);//flowLayout.itemSize = CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));//列の最小間隔を設定するminimumInteritemSpacing = 10;//最小行間隔を設定minimumLineSpacing = 15;//レイアウトの内側余白を設定するsectionInset = UIEdgeInsetsMake(15, 15, 15, 15);//スクロール方向scrollDirection = UICollectionViewScrollDirectionVertical;//flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];//背景色が黒である場合の設定背景色collectionView.backgroundColor = [UIColor whiteColor];//エージェントの設定delegate = self; collectionView.dataSource = self;//[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CELL"]; [collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"]; [self.view addSubview:collectionView]; }

  • //パーティション数を返す
  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; }

  • //パーティションごとに何個のitemがあるか
  • (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _allDataArray.count; }
  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{//UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL"forIndexPath:indexPath]; CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL"forIndexPath:indexPath];//各itemに必要なデータNSDictionary*dic=[_allDataArrayobjectAtIndex:indexPath.item];//画像名NSString*imagesString=[dic objectForKey:@"imageName";cell.imageView.image = [UIImage imageNamed:imageString];//テキストNSString*textString=[dic objectForKey:@"textTable";cell.titleLabel.text = textString; cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/256.0 green:arc4random()%256/256.0 blue:arc4random()%256/256.0 alpha:1];
  • return cell;
    

    }
    //写真のクリック方法
  • (void)collectionView:(UIcollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{NSLog(@「%ldピクチャをクリックしました!!」indexPath.item + 1);

  • }
  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];//Dispose of any resources that can be recreated. }

  • /*

    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
    CustomCollectionViewCell.h中:

    import


    @interface CustomCollectionViewCell : UICollectionViewCell @property (nonatomic,retain)UIImageView *imageView;//画像@property(nonatomic,retain)UILabel*titleLabel;//テキストの表示
    @end
    CustomCollectionViewCell.m中

    import "CustomCollectionViewCell.h"


    @implementation CustomCollectionViewCell
  • (UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc] initWithFrame:self.viewForFirstBaselineLayout.bounds]; [self.contentView addSubview:_imageView]; }return _imageView; }
  • (UILabel *)titleLabel{ if (!_titleLabel) { _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 30, CGRectGetWidth(self.bounds), 30)]; [self.contentView addSubview:_titleLabel]; }return _titleLabel; } @end