首页 > 移动平台 > 详细

ios UICollectionView基本使用

时间:2016-01-24 23:41:34      阅读:372      评论:0      收藏:0      [点我收藏+]

#import <UIKit/UIKit.h>


 


@interface NJProductViewController : UICollectionViewController


//  ‘UICollectionView must be initialized with a non-nil layout parameter‘


// 创建UICollectionView必须传入一个非空的layout的参数


@end




1
#import "NJProductViewController.h" 2 3 #define NJIdentifier @"COLLECTION" 4 5 @interface NJProductViewController () 6 7 @end 8 9 @implementation NJProductViewController 10 11 - (id)init 12 { 13 // UICollectionViewLayout // 布局对象决定了将来CollectionView上每一个Cell显示的方式 14 // 创建一个布局对象 15 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 16 // 设置每一个cell的宽高 (cell在CollectionView中称之为item) 17 layout.itemSize = CGSizeMake(100, 100); 18 // 设置item行与行之间的间隙 19 layout.minimumLineSpacing = 20; 20 // 设置item列与列之间的间隙 21 // layout.minimumInteritemSpacing = 30; 22 // 在初始化的时候传入自己创建的布局对象 23 if (self = [super initWithCollectionViewLayout:layout]) { 24 25 } 26 return self; 27 } 28 29 - (void)viewDidLoad 30 { 31 [super viewDidLoad]; 32 // Do any additional setup after loading the view. 33 34 self.navigationItem.title = @"产品推荐"; 35 36 // 告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中) 37 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NJIdentifier]; 38 } 39 40 41 #pragma mark - 数据源方法 42 // 告诉系统一共有多少组 43 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 44 { 45 return 1; 46 } 47 48 // 告诉系统第section组有多少行 49 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 50 { 51 return 10; 52 } 53 54 // 告诉系统indexPath的第Section组的item行显示什么内容 55 56 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 57 { 58 // indexPath.section;// 第几组 59 // indexPath.item;// 第几个 60 // 从缓存池中获取cell 61 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NJIdentifier forIndexPath:indexPath]; 62 // if (cell == nil) { 63 // cell = [[UICollectionViewCell alloc] init] 64 // } 65 cell.backgroundColor = [UIColor greenColor]; 66 67 return cell; 68 } 69 @end

 

ios UICollectionView基本使用

原文:http://www.cnblogs.com/PJHome/p/5156181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!