UICollectionView 集合视图 的使用

来源:互联网 发布:js数组实例 编辑:程序博客网 时间:2024/05/16 15:26

直接上代码:

////  RootViewController.m////#import "RootViewController.h"#import "CollectionViewCell.h"@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>@property (nonatomic, retain) UICollectionView *collectionView ; // 集合视图@end@implementation RootViewController- (void)dealloc {    [_collectionView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // 1、在创建集合视图对象之前需要创建集合视图的布局类对象,用于对集合视图的单元格做布局。    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];    // 2、为相关属性    // 设置最小行间距    flowLayout.minimumLineSpacing = 5;    // 设置最小列间距    flowLayout.minimumInteritemSpacing = 5;    // 设置 itemSize    flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 40) / 4, 120);    // 设置分区的内边距    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);    // 设置滑动方向,默认是纵向滑动。//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;    // 指定页眉大小    flowLayout.headerReferenceSize = CGSizeMake(200, 60);    // 指定页脚大小    flowLayout.footerReferenceSize = CGSizeMake(200, 40);    self.collectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease];    // 需要制定代理对象    self.collectionView.dataSource = self;    self.collectionView.delegate = self;    // 添加集合视图显示    [self.view addSubview:self.collectionView];    // 为集合视图注册单元格类型    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];    // 为集合视图注册页眉页脚的类型    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];}// 为集合视图的每一个分区指定 item 的数量- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return 100;}// 为集合视图的每一个 Item 指定对应的单元格- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];    cell.titleLable.text = [NSString stringWithFormat:@"(%ld, %ld)", indexPath.section, indexPath.item];//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];    return cell;}// 集合视图的页眉页脚视图使用的是 UICollectionReusableView 或其子类的对象,同时页眉页脚通过 kind 来区分,并且使用专门的重用机制,来完成对页眉页脚视图的管理。- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];        headerView.backgroundColor = [UIColor orangeColor];        return headerView;    }    UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];    footerView.backgroundColor = [UIColor redColor];    return footerView;}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {    UIViewController *viewController = [[UIViewController alloc] init];    [self.navigationController pushViewController:viewController animated:YES];    [viewController release];}- (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
////  CollectionViewCell.h////#import <UIKit/UIKit.h>@interface CollectionViewCell : UICollectionViewCell@property (nonatomic, retain) UILabel *titleLable ; //@end
////  CollectionViewCell.m////#import "CollectionViewCell.h"@implementation CollectionViewCell- (void)dealloc {    [_titleLable release];    [super dealloc];}- (UILabel *)titleLable {    if (!_titleLable) {        self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];        _titleLable.backgroundColor = [UIColor lightGrayColor];        _titleLable.textColor = [UIColor whiteColor];        _titleLable.font = [UIFont systemFontOfSize:16];        _titleLable.textAlignment = NSTextAlignmentCenter;        [self.contentView addSubview:_titleLable];    }    return _titleLable;}@end
1 0
原创粉丝点击