1.Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *hobby;
@property(nonatomic,copy)NSString *address;
@property(nonatomic,copy)NSString *phone;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *age;
@end
2.Student.m
#import "Student.h"
@implementation Student
//这个方法是KVC里负责纠错的方法,只要Key和属性名没有对应上,就会执行这个方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
3.RootViewController.m
#import "RootViewController.h"
#import "Student.h"
#import "Movie.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
//定义model属性
@property(nonatomic,retain)NSMutableArray *modelArr;
@property(nonatomic,retain)NSMutableArray *movieArr;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor cyanColor];
UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.delegate=self;
tableView.dataSource=self;
[self createMovie];
}
-(void)createMovie{
NSString *path=@"/Users/dllo/Desktop/UI/UI10_KVC/UI10_KVC/movielist.txt";
NSData *data=[NSData dataWithContentsOfFile:path];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *movieArr=dic[@"result"];
self.modelArr=[NSMutableArray array];
for (NSDictionary *dic in movieArr) {
Movie *movie=[[Movie alloc] init];
[movie setValuesForKeysWithDictionary:dic];
[self.modelArr addObject:movie];
[movie release];
}
}
-(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:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
Student *stu=self.modelArr[indexPath.row];
cell.textLabel.text=stu.name;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
-(void)cresteData{
NSString *path=[[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
NSMutableArray *arr=[NSMutableArray arrayWithContentsOfFile:path];
NSLog(@"%@",arr);
self.modelArr=[NSMutableArray array];
for (NSDictionary *dic in arr) {
Student *stu=[[Student alloc] init];
[stu setValuesForKeysWithDictionary:dic];
NSLog(@"%@",stu.name);
[self.modelArr addObject:stu];
[stu release];
}
}