MJExtension第三方框架:将字典数据(也就是JSON数据)与Model模型之间的转化
http://www.jianshu.com/p/4782fbd029cc
#import <Foundation/Foundation.h>
@interface ViewControllerModels :NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *icon;
@end
#import "ViewControllerModels.h"
@implementation ViewControllerModels
@end
xib界面内容设置
#import <UIKit/UIKit.h>
@class ViewControllerModels;
@interface YellowView : UIView
@property(nonatomic,strong)ViewControllerModels *models;
@end
#import "YellowView.h"
#import "ViewControllerModels.h"
@interface YellowView()
@property (weak,nonatomic)IBOutletUIImageView *iconImageView;
@property (weak,nonatomic)IBOutletUILabel *nameLb;
@property (weak,nonatomic)IBOutletUIButton *downBtn;
@end
@implementation YellowView
//重写set方法,set方法主要用来赋值
-(void)setModels:(ViewControllerModels *)models{
//对属性进行赋值
_models = models;
//给子控件进行赋值
_iconImageView.image = [UIImageimageNamed:models.icon];
_nameLb.text = models.name;
}
//下载按钮响应内容
- (IBAction)clicked:(id)sender {
//获取控制器的View
UIView *fatherView =self.superview;
//创建遮罩View
UIView *coverView = [[UIViewalloc]initWithFrame:fatherView.frame];
coverView.backgroundColor = [UIColorblackColor];
coverView.alpha =0.4;
[fatherView addSubview:coverView];
//正在下载中......
UILabel *coverLb = [[UILabelalloc]initWithFrame:CGRectMake(0, fatherView.bounds.size.height/3, fatherView.bounds.size.width,20)];
coverLb.text =@"正在下载中......";
coverLb.textAlignment =NSTextAlignmentCenter;
coverLb.textColor = [UIColorwhiteColor];
[fatherView addSubview:coverLb];
//设置动画
[UIViewanimateWithDuration:2animations:^{
coverView.alpha =0.4;
} completion:^(BOOL finished) {
[UIViewanimateWithDuration:1delay:2options:UIViewAnimationOptionCurveEaseInOut animations:^{
coverView.alpha =0;
} completion:^(BOOL finished) {
//按钮修改为禁用
_downBtn.enabled =NO;
_downBtn.backgroundColor = [UIColordarkGrayColor];
[coverLb removeFromSuperview];//移除Label
}];
}];
}
@end
#import "ViewController.h"
#import "YellowView.h"
#define kcolumn 3 //定义列数
@interface ViewController ()
@property (nonatomic,retain)NSArray *imagesArray;
@end
@implementation ViewController
//懒加载
-(NSArray *)imagesArray{
if (_imagesArray ==nil) {
NSString *path = [[NSBundlemainBundle]pathForResource:@"icon"ofType:@"plist"];
NSArray *dataArray = [NSArrayarrayWithContentsOfFile:path];
NSLog(@"dataArray:%@",dataArray);
NSMutableArray *array = [NSMutableArrayarray];
for (NSDictionary *dictin dataArray) {
ViewControllerModels *dataModels = [[ViewControllerModelsalloc]init];
dataModels.name = dict[@"name"];
dataModels.icon = dict[@"icon"];
[array addObject:dataModels];
}
_imagesArray = array;
}
return_imagesArray;
}
- (void)viewDidLoad {
[superviewDidLoad];
NSLog(@"%@",self.imagesArray);
[selfdemo3];
// [self demo2];
// [self data];//获取数据
// [self demo];
}
#pragma mark - 使用xib制作
-(void)demo3{
//获取当前设备大小
UIScreen *mainScreen = [UIScreenmainScreen];
NSLog(@"%f,%f",mainScreen.bounds.size.width,mainScreen.bounds.size.height);
//定义View的宽高
float width =90;
float height =100;
//定义间距
double margin = (mainScreen.bounds.size.width - width*kcolumn)/(kcolumn+1); //列间距
NSLog(@"margin:%lf",margin);
float line =30; //行间距
for (int i =0; i <_imagesArray.count; i++) {
//获取行索引和列索引
NSInteger rowIndex = i/kcolumn; //行
NSInteger columnIndex = i%kcolumn;//列
NSLog(@"第%ld行,第%ld列",rowIndex,columnIndex);
float x = margin*(columnIndex+1)+width*columnIndex;
float y = line*(rowIndex+1)+height*rowIndex;
//获取xib上的View;
YellowView *yellowView = [[[NSBundlemainBundle]loadNibNamed:@"YellowView"owner:niloptions:nil]firstObject];
[yellowView setFrame:CGRectMake(x, y, width, height)];
[self.viewaddSubview:yellowView];
//创建字典模型,存储数据
ViewControllerModels *dataModels = [ViewControllerModelsnew];
dataModels = self.imagesArray[i];
//赋值
yellowView.models = dataModels;
//根据tag值,取出UI控件,不建议使用
// UIImageView *iconImageView =(UIImageView *)[yellowView viewWithTag:10];
// iconImageView.image = [UIImage imageNamed:dataModels.icon];
//
// UILabel *nameLb = (UILabel *)[yellowView viewWithTag:11];
// nameLb.text = dataModels.name;
}
}
-(void)demo2{
//获取当前设备大小
UIScreen *mainScreen = [UIScreenmainScreen];
NSLog(@"%f,%f",mainScreen.bounds.size.width,mainScreen.bounds.size.height);
//定义View的宽高
float width =90;
float height =100;
//定义间距
double margin = (mainScreen.bounds.size.width - width*kcolumn)/(kcolumn+1); //列间距
NSLog(@"margin:%lf",margin);
float line =30; //行间距
for (int i =0; i <_imagesArray.count; i++) {
//获取行索引和列索引
NSInteger rowIndex = i/kcolumn; //行
NSInteger columnIndex = i%kcolumn;//列
NSLog(@"第%ld行,第%ld列",rowIndex,columnIndex);
float x = margin*(columnIndex+1)+width*columnIndex;
float y = line*(rowIndex+1)+height*rowIndex;
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(x, y, width, height)];
// view.backgroundColor = [UIColor yellowColor];
[self.viewaddSubview:view];
//创建字典模型,存储数据
ViewControllerModels *dataModels = [ViewControllerModelsnew];
dataModels = self.imagesArray[i];
//设置图片
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(20,2,50,50)];
NSString *imageStr = dataModels.icon;
imageView.image = [UIImageimageNamed:imageStr];
// imageView.backgroundColor = [UIColor redColor];
[view addSubview:imageView];
//文字
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(imageView.frame)+2, width,15)];
label.text = dataModels.name;
label.font = [UIFontsystemFontOfSize:10];
label.textAlignment =NSTextAlignmentCenter;
[view addSubview:label];
//下载按纽
UIButton *downBtn = [[UIButtonalloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(label.frame)+3, width-20,20)];
[downBtn setTitle:@"下载"forState:UIControlStateNormal];
downBtn.titleLabel.textAlignment =NSTextAlignmentCenter;
downBtn.backgroundColor = [UIColorgreenColor];
[view addSubview:downBtn];
}
}
#pragma mark -
-(void)data{
NSString *path = [[NSBundlemainBundle]pathForResource:@"icon.plist"ofType:nil];
NSArray *dataArray = [NSArrayarrayWithContentsOfFile:path];
NSLog(@"%@",dataArray);
NSMutableArray *array = [NSMutableArrayarray];
for (int i=0; i<dataArray.count; i++) {
NSDictionary *dict = [NSDictionarydictionary];
dict = dataArray[i];
[array addObject:dict];
}
_imagesArray = [[NSArrayalloc]initWithArray:array];
NSLog(@"_imageArray:%@",_imagesArray);
}
-(void)demo{
//获取当前设备大小
UIScreen *mainScreen = [UIScreenmainScreen];
NSLog(@"%f,%f",mainScreen.bounds.size.width,mainScreen.bounds.size.height);
//定义View的宽高
float width =90;
float height =100;
//定义间距
double margin = (mainScreen.bounds.size.width - width*kcolumn)/(kcolumn+1); //列间距
NSLog(@"margin:%lf",margin);
float line =30; //行间距
int num =0;
for (int j=0; j<4; j++) { //确定行数
for (int i =0 ; i<kcolumn; i++) {//确定列数
float x = margin*(i+1)+width*i;
float y = line*(j+1)+height*j;
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(x, y, width, height)];
// view.backgroundColor = [UIColor yellowColor];
[self.viewaddSubview:view];
//设置图片
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(20,2,50,50)];
NSString *imageStr = [_imagesArray[num]valueForKey:@"icon"];
imageView.image = [UIImageimageNamed:imageStr];
// imageView.backgroundColor = [UIColor redColor];
[view addSubview:imageView];
//文字
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(imageView.frame)+2, width,15)];
label.text = [_imagesArray[num]valueForKey:@"name"];
label.font = [UIFontsystemFontOfSize:10];
label.textAlignment =NSTextAlignmentCenter;
[view addSubview:label];
//下载按纽
UIButton *downBtn = [[UIButtonalloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(label.frame)+3, width-20,20)];
[downBtn setTitle:@"下载"forState:UIControlStateNormal];
downBtn.titleLabel.textAlignment =NSTextAlignmentCenter;
downBtn.backgroundColor = [UIColorgreenColor];
[view addSubview:downBtn];
num++;
}
}
}
@end
#import <Foundation/Foundation.h>
@interface HomeImageModels : NSObject
@property(retain,nonatomic)NSString *id;
@property(retain,nonatomic)NSString *url;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
#import "HomeImageModels.h"
@implementation HomeImageModels
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [superinit];
if (self) {
self.id =[dictionaryvalueForKey:@"id"];
self.url =[dictionaryvalueForKey:@"url"];
}
returnself;
}
@end
-(void)advertisement{
//获取2张广告图片路径
NSString *urlStr=[NSStringstringWithFormat:@"%@/help/getGuanggao",HOME_PAGE_HOST];
NSMutableDictionary *dic=[NSMutableDictionarydictionary];
[dic setValue:NSUSER_DEF(@"USERID")forKey:@"userId"];
//实例化网络工具管理类
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
[manager GET:urlStrparameters:dicsuccess:^(NSURLSessionDataTask *task,id responseObject) {
NSLog(@"%@",responseObject);
NSString *reCode = [NSStringstringWithFormat:@"%@",[responseObjectvalueForKey:@"resCode"]];
if ([reCodeisEqualToString:@"0"]) {
NSArray *dataArr = responseObject[@"resList"];
imageArray = [NSMutableArrayarray];
for(NSDictionary *dataDicin dataArr){
HomeImageModels *image = [[HomeImageModelsalloc]initWithDictionary:dataDic];
[imageArrayaddObject:image];
}
NSLog(@"imageArray %@",imageArray);
[selfshowMybannerPlayer];
}
} failure:^(NSURLSessionDataTask *task,NSError *error) {
NSLog(@"error=====%@",error);
}];
}