标签:style blog color os io for ar cti
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类
与UICollectionView有关的三个协议:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
几个常用到得方法(需遵守协议)
- (void)viewDidLoad { [super viewDidLoad]; //创建一个布局方式 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.headerReferenceSize = CGSizeMake(320, 50);//header的size layout.footerReferenceSize = CGSizeMake(320, 20); layout.sectionInset = UIEdgeInsetsMake(0, 0, 20, 0); //创建一个集合视图 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; collectionView.dataSource = self; collectionView.delegate = self; [collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"]; //注册header [collectionView registerClass:[CustomHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; //注册footer 增补视图 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"]; [self.view addSubview:collectionView]; [layout release]; [collectionView release]; // Do any additional setup after loading the view. } //定义展示的Section的个数 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } //定义展示的UICollectionViewCell的个数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } //每个UICollectionViewCell展示的内容 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; cell.backgroundColor = [UIColor orangeColor]; return cell; } //定义每个UICollectionView 中cell的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100, 200); } //增补视图header与footer - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (kind == UICollectionElementKindSectionHeader) { CustomHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath]; view.backgroundColor = [UIColor greenColor]; return view; }else { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath]; view.backgroundColor = [UIColor redColor]; return view; } }
对UICollectionView的学习,布布扣,bubuko.com
标签:style blog color os io for ar cti
原文地址:http://www.cnblogs.com/limicheng/p/3893003.html