ios UICollectionView基本使用

 
  

#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

 

转载于:https://www.cnblogs.com/PJHome/p/5156181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值