#import "HomeViewController.h"
#import "AppCell.h"
#import "AppModel.h"
@interface HomeViewController ()<UICollectionViewDataSource>
//定义一个数组
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end
@implementation HomeViewController
#pragma mark - 加载数据
- (NSArray *)dataArray{
if (_dataArray ==nil) {
//路径
NSString *path = [[NSBundlemainBundle]pathForResource:@"icon"ofType:@"plist"];
//读取
NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];
//可变
NSMutableArray *mutableArray = [NSMutableArrayarray];
//转换
for (NSDictionary *dictin tempArray) {
AppModel *appModel = [AppModelappModelWithDict:dict];
[mutableArray addObject:appModel];
NSLog(@"appModel = %@",appModel);
}
//赋值
_dataArray = mutableArray;
}
return_dataArray;
}
//定义重置标识符
static NSString *identifier =@"collectionCell";
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//1、实例化一个collectionViewFlowLayout
_flowLayout = [[UICollectionViewFlowLayoutalloc]init];
//设置大小
_flowLayout.itemSize =CGSizeMake(100,100);
//修改cell距离view的边距
_flowLayout.sectionInset =UIEdgeInsetsMake(60,20, 0,20);
//修改滚动的方向
// _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//水平
//行列间距设置与滚动方向相关
//最小列间距
_flowLayout.minimumInteritemSpacing =10;
//最小行间距
_flowLayout.minimumLineSpacing =20;
//2、实例化一个collectionView
UICollectionView *collectionView = [[UICollectionViewalloc]initWithFrame:self.view.framecollectionViewLayout:_flowLayout];
//3、注册一个cell
[collectionView registerClass:[AppCellclass] forCellWithReuseIdentifier:identifier];
//4、设置数据源代理
collectionView.dataSource =self;
//5、添加到View上
[self.viewaddSubview:collectionView];
//6、设置背景色
collectionView.backgroundColor = [UIColorwhiteColor];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
returnself.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//从缓存池中取
AppCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:identifierforIndexPath:indexPath];
//取出appModel
AppModel *appModel =self.dataArray[indexPath.row];
cell.appModel = appModel;
return cell;
}
//当视图加载完成之后调用
- (void)viewDidAppear:(BOOL)animated{
//修改cell中的size
_flowLayout.itemSize =CGSizeMake(200,200);
}
#import <UIKit/UIKit.h>
@class AppModel;
@interface AppCell :UICollectionViewCell
@property (nonatomic,retain)UIImageView *iconImageView;
@property (nonatomic,weak)UILabel *nameLabel;
@property (nonatomic,strong)AppModel *appModel;
@end
#import "AppCell.h"
#import "AppModel.h"
@implementation AppCell
- (id)initWithFrame:(CGRect)frame{
if (self = [superinitWithFrame:frame]){
//设置cell背景色
self.backgroundColor = [UIColorredColor];
//打印frame大小
NSLog(@"%@",NSStringFromCGRect(frame));
//取cell的大小
// CGSize cellSize = self.contentView.frame.size;
//
// //添加imageView
// CGFloat iconWidth = cellSize.width*0.6;
// CGFloat iconX = (cellSize.width -iconWidth)/2;
// _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(iconX, 0, iconWidth, iconWidth)];
UIImageView *iconImageView = [[UIImageViewalloc]init];
self.iconImageView = iconImageView;
[self.contentViewaddSubview:iconImageView];
//取imageView的Y值
// CGFloat imageViewY = CGRectGetMaxY(_iconImageView.frame);
//
// //添加Label
// UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imageViewY, cellSize.width, 20)];
UILabel *nameLabel = [[UILabelalloc]init];
_nameLabel = nameLabel;
//添加Label的属性
nameLabel.font = [UIFontsystemFontOfSize:13];
nameLabel.textAlignment =NSTextAlignmentCenter;
nameLabel.textColor = [UIColorblackColor];
[self.contentViewaddSubview:nameLabel];
}
returnself;
}
//如果子控件需要根据父控件的变化而变化时,在layoutSubviews中设置,否则可在上面设置,父控件变化,子控件不变
//当View的size发生变化时候调用
- (void)layoutSubviews{
[superlayoutSubviews];
//取cell的大小
CGSize cellSize =self.contentView.frame.size;
//添加imageView
CGFloat iconWidth = cellSize.width*0.6;
CGFloat iconX = (cellSize.width -iconWidth)/2;
_iconImageView.frame =CGRectMake(iconX, 0, iconWidth, iconWidth);
//取imageView的Y值
CGFloat imageViewY =CGRectGetMaxY(_iconImageView.frame);
//添加Label
_nameLabel.frame =CGRectMake(0, imageViewY, cellSize.width,20);
}
//重写set方法
- (void)setAppModel:(AppModel *)appModel{
_appModel = appModel;
//对子控件进行赋值
_iconImageView.image = [UIImageimageNamed:appModel.icon];
_nameLabel.text = appModel.name;
NSLog(@"%@",_nameLabel.text);
}
@end
@interface AppModel : NSObject
@property (nonatomic,copy)NSString *icon;
@property (nonatomic,copy)NSString *name;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appModelWithDict:(NSDictionary *)dict;
@end
#import "AppModel.h"
@implementation AppModel
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [superinit]){
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
+ (instancetype)appModelWithDict:(NSDictionary *)dict{
return [[selfalloc]initWithDict:dict];
}
@end