IOS複習 九宮格 字典轉模型與懶加載 xib界面控件代碼賦值 模擬下載過程 MJExtension

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

    }];

}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章