UI总结-KVC赋值

               UI总结-KVC赋值

      在实际的项目阶段,后台给我们的数据都是以字典的形式.我们在拿到数据的时候要如何操作才能将数据转化为我们能用的数据,这时候我们就要用到KVC赋值了.

Viewcontroller.m文件:

#import "ViewController.h"

#import "Student.h"

@interface ViewController ()

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic ,retain)NSMutableArray *stuArr;

@property(nonatomic, retain)NSMutableArray *modelArr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self creatData];

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

[self.view addSubview:self.tableView];

self.tableView.dataSource =self;

self.tableView.delegate = self;

// Key-Value-Coding

Student *stu = [[Student alloc]init];

[stu setValue:@"鸣人" forKey:@"name"];

NSLog(@"%@",stu.name);

NSLog(@"%@",[stu valueForKey:@"name"]);

self.modelArr = [NSMutableArray array];

for (NSDictionary *dic in self.stuArr) {

Student *stu = [[Student alloc]init];

//kvc进行赋值

[stu setValuesForKeysWithDictionary:dic];

NSLog(@"%@",stu.name);

[self.modelArr addObject:stu];

}

}

-(void)creatData{

NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];

self.stuArr = [NSMutableArray arrayWithContentsOfFile:path];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.modelArr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *reuse = @"reuse";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];

}

Student * stu = self.modelArr[indexPath.row];

cell.textLabel.text = stu.name;

return cell;

}

Student.h文件:

#import<Foundation/Foundation.h>

@interface Student : NSObject

@property(nonatomic, copy)NSString *name;

@property(nonatomic, copy)NSString *age;

@property(nonatomic, copy)NSString *phone;

@property(nonatomic, copy)NSString *address;

@property(nonatomic, copy)NSString *hobby;

@end

Student.m文件:

#import "Student.h"

@implementation Student

//KVC的容错方法

//只要在赋值过程中,没有找到对应的属性,就会自动调用这个方法,这个方法里如果没有其他操作可什么都不写.

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

@end

运行结果:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的...
    Jt_Self阅读 4,104评论 0 4
  • 设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的...
    如果我们是朋友阅读 3,907评论 0 9
  • 设计模式是什么? 你知道哪些设计模式,并简要叙述?设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的事...
    irenb阅读 10,609评论 1 21
  • 今晚与儿子睡前卧谈,我提议: “咱俩说说今天值得感恩的5件事吧!” 儿子问:“什么是感恩?” 我说“就是特别特别让...
    点亮人生阅读 1,722评论 1 1
  • 这天逢集,小胡子老王又扛着一个大瓦缸去了镇子上。他今天要把瓦缸卖个好价钱,给孩子买个糖饼子吃。 城门口的几个小卒拿...
    铃木结爱阅读 3,926评论 0 1