IOS学习 collectionView 创建应用管理 涉及单独用类方法从plist文件中取数据

#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];

        

        //imageViewY

//        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中设置,否则可在上面设置,父控件变化,子控件不变

//Viewsize发生变化时候调用

- (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);

    

    //imageViewY

    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





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值