标签:
1.设置ViewController来管理视图
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; ViewController *rootVC = [[ViewController alloc]init]; //导航控制器 UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootVC]; //将导航控制器设置为主视图控制器根视图控制器 self.window.rootViewController = navi; //释放所有权 [navi release]; [rootVC release]; return YES; }
2.在导航控制器中对ControllerView的实现
这里使用的都是系统提供的cell,页眉和页脚,存在局限性,一般情况下,都是用的自定义的,在这里为了简单就写使用了这种写法
#import "ViewController.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.title = @"集合视图"; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; int colums = 3; CGFloat viewWidth = self.view.frame.size.width; //配置属性 //1.设置每个item的大小 layout.itemSize = CGSizeMake((viewWidth - (colums - 1) * 60)/colums , 120); //2.设置缩进量(上下左右四个方向) layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //3.设置每行的最小行间距 layout.minimumLineSpacing = 30; //4.设置item之间的最小边距 layout.minimumInteritemSpacing = 30; //5.设置滑动的方向,分为横向滑动和竖向滑动,默认是纵向滑动 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //6.设置页眉(横向滑动情况下默认高为整个屏幕高,纵向滑动情况下默认宽是整个屏幕宽) layout.headerReferenceSize = CGSizeMake(30, 30); //7.设置页脚 layout.footerReferenceSize = CGSizeMake(60, 60); //建立UICollectionView UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout]; //默认背景色为黑色 collectionView.backgroundColor = [UIColor whiteColor]; //设置代理监听事件 collectionView.delegate = self; //提供数据 collectionView.dataSource = self; [self.view addSubview:collectionView]; [collectionView release]; [layout release]; //注册cell [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; //注册页眉 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; //注册页脚 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"]; } //section的个数 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 3; } //一个section的item数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10; } //返回cell - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //返回页眉 UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath]; header.backgroundColor = [UIColor yellowColor]; return header; } else { //返回页脚 UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath]; footer.backgroundColor = [UIColor orangeColor]; return footer; } }
关于UICollectionView在MRC下的纯代码实现之一(在UIViewController中实现)
标签:
原文地址:http://www.cnblogs.com/dubuladuo/p/4857736.html