#import “ViewController.h”
#import “AFNetworking.h”
#import “Model.h”
#import “TableViewCell.h”
@interface ViewController ()<UITableViewDelegate , UITableViewDataSource>
@property(nonatomic,strong) UITableView *tableview;
@property(nonatomic , strong)NSMutableArray *array;
@end
@implementation ViewController
-
(UITableView *)tableview{
if (!_tableview) {
_tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableview.delegate =self;
_tableview.dataSource =self;
}
//注册
[_tableview registerNib:[UINib nibWithNibName:@“TableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];
return _tableview;
} -
(void)viewDidLoad {
[super viewDidLoad];
self.title = @“我的考试”;
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255/255.0 green:202/255.0 blue:92/255.0 alpha:1];
self.array = [NSMutableArray array];
[self.view addSubview:self.tableview];
[self get];
}
-(void)get{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@“text/plain”];
[manager GET:@“http://127.0.0.1/Jason/first.json” parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
NSArray *arr = responseObject;
// 添加数据
for (NSDictionary *dic in arr) {
Model *model = [Model new];
[model setValuesForKeysWithDictionary:dic];
[self.array addObject:model];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@出错",error);
}];
}
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.array.count;
} -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];
Model *model =self.array[indexPath.row];
return cell;
} -
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@end