IOS学习 collectionView 创建应用管理 涉及单独用类方法从plist文件中取数据
来源:互联网 发布:access录入新数据 编辑:程序博客网 时间:2024/05/29 17:26
#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
- IOS学习 collectionView 创建应用管理 涉及单独用类方法从plist文件中取数据
- ios开发从.plist文件中获取数据
- IOS之从plist文件中获取数据
- plist文件取数据
- iOS-从plist文件获取数据
- ios从.plist文件中加载资源
- iOS-生成plist文件,在项目中代码创建plist
- OC从plist文件中获取数据
- IOS学习 collectionView 的创建
- 使用 UITableView 创建表格应用——从plist文件加载并显示数据(转)
- IOS从.plist文件中读取内容,创建一个图片浏览器
- 取plist文件的方法
- iOS plist文件中取值遇到的陷阱
- [iOS 转]iOS开发中怎么创建Plist文件
- iOS -- 用plist文件存取数据
- ios开发(plist文件数据加载) 使用数据模型的方法加载plist文件中的数据
- cocos2dx-深度解析plist文件(一)(游戏对象的数据如何从plist创建获取)
- iOS Plist文件的创建
- ok210使用-自己做NFS文件系统
- ZrcListView
- Android中IntentService详解
- 求最大公约数
- iOS开发 JSONKit解析
- IOS学习 collectionView 创建应用管理 涉及单独用类方法从plist文件中取数据
- 学习卡片(1):KNN
- Java 运算符知识整理 学习笔记
- Day1完跪,AFO
- 分离式编译模式
- 堆排算法实现
- CSS字体
- DAY5 6吧
- 哈夫曼编码