UI總結(jié)-KVC賦值

? ? ? ? ? ? ? ?UI總結(jié)-KVC賦值

? ? ? 在實(shí)際的項(xiàng)目階段,后臺(tái)給我們的數(shù)據(jù)都是以字典的形式.我們?cè)谀玫綌?shù)據(jù)的時(shí)候要如何操作才能將數(shù)據(jù)轉(zhuǎn)化為我們能用的數(shù)據(jù),這時(shí)候我們就要用到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進(jìn)行賦值

[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的容錯(cuò)方法

//只要在賦值過(guò)程中,沒(méi)有找到對(duì)應(yīng)的屬性,就會(huì)自動(dòng)調(diào)用這個(gè)方法,這個(gè)方法里如果沒(méi)有其他操作可什么都不寫.

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

}

@end

運(yùn)行結(jié)果:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 設(shè)計(jì)模式是什么? 你知道哪些設(shè)計(jì)模式,并簡(jiǎn)要敘述? 設(shè)計(jì)模式是一種編碼經(jīng)驗(yàn),就是用比較成熟的邏輯去處理某一種類型的...
    Jt_Self閱讀 836評(píng)論 0 4
  • 設(shè)計(jì)模式是什么? 你知道哪些設(shè)計(jì)模式,并簡(jiǎn)要敘述? 設(shè)計(jì)模式是一種編碼經(jīng)驗(yàn),就是用比較成熟的邏輯去處理某一種類型的...
    如果我們是朋友閱讀 743評(píng)論 0 9
  • 設(shè)計(jì)模式是什么? 你知道哪些設(shè)計(jì)模式,并簡(jiǎn)要敘述?設(shè)計(jì)模式是一種編碼經(jīng)驗(yàn),就是用比較成熟的邏輯去處理某一種類型的事...
    irenb閱讀 5,385評(píng)論 1 21
  • 今晚與兒子睡前臥談,我提議: “咱倆說(shuō)說(shuō)今天值得感恩的5件事吧!” 兒子問(wèn):“什么是感恩?” 我說(shuō)“就是特別特別讓...
    點(diǎn)亮人生閱讀 317評(píng)論 1 1
  • 這天逢集,小胡子老王又扛著一個(gè)大瓦缸去了鎮(zhèn)子上。他今天要把瓦缸賣個(gè)好價(jià)錢,給孩子買個(gè)糖餅子吃。 城門口的幾個(gè)小卒拿...
    鈴木結(jié)愛閱讀 798評(píng)論 0 1

友情鏈接更多精彩內(nèi)容