
最终效果图:
代码片段:
DealListController继承自UICollectionVIEwController
self.vIEw已经包含了一个UICollectionVIEw
并且数据源和代理已经是当前控制器self
//// DealListController.m// 帅哥_团购//// Created by beyond on 14-8-14.// copyright (c) 2014年 com.beyond. All rights reserved.// 点击dock上面的【团购】按钮对应的控制器,上面是导航栏,导航栏右边是searchbar,导航栏左边是一个大按钮(topMenu)(内部由三个小按钮组成<topMenuItem>)#import "DealListController.h"// 导航栏左边是一个大按钮(顶部菜单)#import "topMenu.h"#define kItemW 250#define kItemH 250@implementation DealListController// 覆盖控制器的init方法- (ID)init{ // 创建一个流布局 UICollectionVIEwFlowLayout *layout = [[UICollectionVIEwFlowLayout alloc] init]; // 设置流布局里面的每一个格子宽和高,即每一个网格的尺寸 layout.itemSize = CGSizeMake(kItemW,kItemH); // 调用父类UICollectionVIEwController的initWithCollectionVIEwLayout方法,(self这儿找不到,会到父类里去找方法) return [self initWithCollectionVIEwLayout:layout];}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; // 1.顶部导航栏的基本设置 [self setNavigationbar]; // 2.collectionVIEw的基本设置 [self setCollectionVIEw];}// 2.顶部导航栏的基本设置- (voID)setNavigationbar{ // 1.右边的搜索框 UISearchbar *s = [[UISearchbar alloc] init]; s.frame = CGRectMake(0,210,35); s.placeholder = @"请输入商品名、地址等"; self.navigationItem.rightbarbuttonItem = [[UIbarbuttonItem alloc] initWithCustomVIEw:s]; // 2.左边的菜单栏 topMenu *top = [[topMenu alloc] init]; // 重要,topMenu里面的item点击后,创建的PopMenu将要添加到哪儿去???就是本控制器的vIEw top.controllerVIEw = self.vIEw; self.navigationItem.leftbarbuttonItem = [[UIbarbuttonItem alloc] initWithCustomVIEw:top]; }// 3.collectionVIEw的基本设置- (voID)setCollectionVIEw{ // 1.设置collectionVIEw的背景色,(不像tableVIEwController,本控制器的vIEw是UIVIEw,在UIVIEw里面再添加的collectionVIEw) self.collectionVIEw.backgroundcolor = kGlobalBg; // 2.重要~~~注册cell要用到的xib [self.collectionVIEw registerNib:[UINib nibWithNibname:@"DealCell" bundle:nil] forCellWithReuseIDentifIEr:@"DealCell"]; // 3.设置collectionVIEw永远支持垂直滚动,为下拉刷新准备(d簧) self.collectionVIEw.alwaysBounceVertical = YES; }// 4.重要~~~因为在控制器创建时,宽默认是768,高默认是1024,不管横竖屏// 只有在vIEwWillAppear和vIEwDIDAppear方法中,可以取得vIEw最准确的(即实际的)宽和高(wIDth和height)- (voID)vIEwWillAppear:(BOol)animated{ // 默认计算layout [self dIDRotateFromInterfaceOrIEntation:0];}#pragma mark - 父类方法// 拦截,屏幕即将旋转的时候调用(控制器监控屏幕旋转)- (voID)willRotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)toInterfaceOrIEntation duration:(NSTimeInterval)duration{ log(@"屏幕即将旋转");}// 拦截,屏幕旋转完毕的时候调用- (voID)dIDRotateFromInterfaceOrIEntation:(UIInterfaceOrIEntation)fromInterfaceOrIEntation{ // 1.取出创建CollectionVIEwController时传入的的UICollectionVIEwFlowLayout UICollectionVIEwFlowLayout *layout = (UICollectionVIEwFlowLayout *)self.collectionVIEw.collectionVIEwLayout; // 2.计算间距 CGfloat v = 0; CGfloat h = 0; CGfloat height = self.vIEw.frame.size.height -44; CGfloat wIDth = self.vIEw.frame.size.wIDth; if (UIInterfaceOrIEntationIsLandscape(self.interfaceOrIEntation) ) { // 横屏的间距 v = (height - 2 * kItemH) / 3; h = (wIDth - 3 * kItemW) / 4; } else { // 竖屏的间距 v = (height - 3 * kItemH) / 4; h = (wIDth - 2 * kItemW) / 3; } // 3.动画调整格子之间的距离 [UIVIEw animateWithDuration:4.0 animations:^{ // 上 左 下 右 四个方向的margin layout.sectionInset = UIEdgeInsetsMake(v,h,v,h); // 每一行之间的间距 layout.minimumlinespacing = v; }];}#pragma mark - collectionVIEw代理方法// 共有多少个Item(就是格子Cube)- (NSInteger)collectionVIEw:(UICollectionVIEw *)collectionVIEw numberOfItemsInSection:(NSInteger)section{ return 6;}// 生成每一个独一无二的格子- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static Nsstring *cellID = @"DealCell"; UICollectionVIEwCell *cell = [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:cellID forIndexPath:indexPath]; // 设置独一无二的数据 return cell;} CollectionVIEw中的格子一般外观一致,可以用xib
DealCell.xib
总结以上是内存溢出为你收集整理的iOS_21团购_UICollectionView的基本使用全部内容,希望文章能够帮你解决iOS_21团购_UICollectionView的基本使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)