1 #import <UIKit/UIKit.h> 2 3 @interface NJProductViewController : UICollectionViewController 4 // ‘UICollectionView must be initialized with a non-nil layout parameter‘ 5 // 创建UICollectionView必须传入一个非空的layout的参数 6 @end 7 8 9 10 11 #import "NJProductViewController.h" 12 #import "NJProduct.h" 13 #import "NJProductItem.h" 14 15 #define NJIdentifier @"COLLECTION" 16 17 @interface NJProductViewController () 18 19 // 定义数组保存所有产品模型 20 @property (nonatomic, strong) NSArray *products; 21 @end 22 23 @implementation NJProductViewController 24 #pragma mark - 懒加载 25 - (NSArray *)products 26 { 27 if (_products == nil) { 28 // 1.加载json中的数据 29 // 2.将json中的数据转换为模型 30 // 2.1获取json文件的全路径 31 NSString *path = [[NSBundle mainBundle] pathForResource:@"product.json" ofType:nil]; 32 // 2.2根据全路径加载json文件到nsdata中 33 NSData *data = [NSData dataWithContentsOfFile:path]; 34 // 2.3将加载进来的nsdata数据转换为OC中对应的对象 35 NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL]; 36 // NSLog(@"%@", dictArray); 37 // 3.将转换后的数据传递给products 38 NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count]; 39 for (NSDictionary *dict in dictArray) { 40 NJProduct *product = [NJProduct productWithDict:dict]; 41 [models addObject:product]; 42 } 43 _products = models; 44 } 45 // 4.返回products 46 return _products; 47 } 48 49 #pragma mark - 初始化方法 50 - (id)init 51 { 52 // UICollectionViewLayout // 布局对象决定了将来CollectionView上每一个Cell显示的方式 53 // 创建一个布局对象 54 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 55 // 设置每一个cell的宽高 (cell在CollectionView中称之为item) 56 layout.itemSize = CGSizeMake(80, 80); 57 // 设置item行与行之间的间隙 58 layout.minimumLineSpacing = 10; 59 // 设置item列与列之间的间隙 60 layout.minimumInteritemSpacing = 0; 61 // 设置CollectionView距离上边 62 // layout.headerReferenceSize = CGSizeMake(0, 100); 63 // layout.footerReferenceSize = CGSizeMake(0, 200); 64 65 // 设置CollectionView内容部分距离控制器view上下左右的边距 66 // 上/左/下/右 67 layout.sectionInset = UIEdgeInsetsMake(layout.minimumLineSpacing, 0, 0, 0); 68 69 // 在初始化的时候传入自己创建的布局对象 70 if (self = [super initWithCollectionViewLayout:layout]) { 71 72 } 73 return self; 74 } 75 76 - (void)viewDidLoad 77 { 78 [super viewDidLoad]; 79 80 self.navigationItem.title = @"产品推荐"; 81 82 // 告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中) 83 // [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NJIdentifier]; 84 // self.products; 85 86 // 如果item(cell) 视同xib描述的, 就要先注册xib 87 UINib *nib = [UINib nibWithNibName:@"NJProductItem" bundle:nil]; 88 [self.collectionView registerNib:nib forCellWithReuseIdentifier:NJIdentifier]; 89 90 // 设置控制器view的背景颜色 91 self.collectionView.backgroundColor = [UIColor whiteColor]; 92 } 93 94 #pragma mark - 数据源方法 95 // 告诉系统一共有多少组 96 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 97 { 98 return 1; 99 } 100 101 // 告诉系统第section组有多少行 102 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 103 { 104 return 10; 105 } 106 107 // 告诉系统indexPath的第Section组的item行显示什么内容 108 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 109 { 110 // indexPath.section;// 第几组 111 // indexPath.item;// 第几个 112 113 // 1.从缓存池中获取cell 114 NJProductItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NJIdentifier forIndexPath:indexPath]; 115 116 // 2.设置数据 117 cell.product = self.products[indexPath.item]; 118 119 // 3.返回cell 120 return cell; 121 } 122 123 #pragma mark- UICollectionViewDelegate 124 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 125 { 126 // 1.获取点击的那一个item对应的模型 127 NJProduct *product = self.products[indexPath.item]; 128 NSLog(@"%@", product.title); 129 } 130 @end
原文:http://www.cnblogs.com/PJHome/p/5156250.html