zoukankan      html  css  js  c++  java
  • 字典转模型

    //
    //  heroObject.m
    //  herosList
    //
    //  Created by xin on 15/4/9.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "heroObject.h"
    
    @implementation heroObject
    
    -(instancetype)initWithDict:(NSDictionary *)dict{
        self = [super init];
        if (self) {
            [self setValuesForKeysWithDictionary:dict];
        }
        return self;
    }
    
    +(instancetype)heroWithDict:(NSDictionary *)dict{
        return [[self alloc]initWithDict:dict];
    }
    
    @end
    

      

    //
    //  heroObject.h
    //  herosList
    //
    //  Created by xin on 15/4/9.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface heroObject : NSObject
    
    //image
    @property (nonatomic,copy) NSString *icon;
    //description
    @property (nonatomic,copy) NSString *intro;
    //title
    @property (nonatomic,copy) NSString *name;
    
    +(instancetype)heroWithDict:(NSDictionary *)dict;
    
    @end
    

      

    //
    //  ViewController.m
    //  herosList
    //
    //  Created by xin on 15/4/9.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "heroObject.h"
    
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @property (nonatomic,strong) NSArray *dict;
    
    @end
    
    @implementation ViewController
    
    -(NSArray *)dict{
        if(_dict == nil){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
            NSArray *arr = [NSArray arrayWithContentsOfFile:path];
            
            NSMutableArray *arrayM = [NSMutableArray array];
            
            for (NSDictionary *dict in arr) {
                heroObject *hero = [heroObject heroWithDict:dict];
                [arrayM addObject:hero];
            }
            
            _dict = [arrayM copy];
            
        }
        return _dict;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.dataSource = self;
        //self.tableView.rowHeight = 60;
        self.tableView.delegate = self;
    }
    
    /*
     * 组
     */
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dict.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        heroObject *hero = self.dict[indexPath.row];
        cell.textLabel.text = hero.name;
        cell.detailTextLabel.text = hero.intro;
        cell.imageView.image =[UIImage imageNamed:hero.icon];
        return cell;
    }
    
    -(BOOL)prefersStatusBarHidden{
        return YES;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.row == 0){
            return 100;
        }else{
            return 60;
        }
    }
    
    @end
    

      

  • 相关阅读:
    尝试使用word发布博客
    设计模式学习系列7 建造者模式
    设计模式学习系列6 原型模式(prototype)
    最近比较闲
    提高程序运行效率的10个简单方法(转)
    设计模式学习系列5 工厂模式
    【LINUX/UNIX网络编程】之使用消息队列,信号量和命名管道实现的多进程服务器(多人群聊系统)
    三十分钟掌握STL
    在python包管理中使用easy_install软件的步骤
    【转】推荐给大家的7本游戏开发书
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4412549.html
Copyright © 2011-2022 走看看