MODEL
#import@interface Model : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSNumber *online;
#import "ViewController.h"#import "Model.h"
@interface ViewController (){
UITableView *tv;
NSMutableArray *mutableArray;
// NSMutableArray *arr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
tv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
mutableArray = [NSMutableArray array];
// 从plist文件中拿到数据
// NSBundle * bundle = [NSBundle mainBundle];
// NSString * path = [bundle pathForResource:@"friends" ofType:@"plist"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"plist"];
NSLog(@"%@",path);
NSArray *array = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in array)
{
// 取出的dic必须通过模型化再传入到mutableArray中
Model *model = [[Model alloc]init];
model.name = dict[@"name"];
model.online = dict[@"online"];
[mutableArray addObject:model];
}
array = mutableArray;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mutableArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
Model *model = [[Model alloc]init];
model = mutableArray[indexPath.row];
NSLog(@"=====%@",model);
cell.textLabel.text = model.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",model.online];
return cell;
}