导入框架
pod ‘AFNetworking’
pod 'SVProgressHUD
#import “fourViewController.h”
#import “fiveViewController.h”
#import <SVProgressHUD.h>
#import <AFNetworking.h>
#import “Model.h”
@interface fourViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *table;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end
@implementation fourViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“朋友” style:UIBarButtonItemStylePlain target:self action:@selector(click)];
self.dataArr=[[NSMutableArray alloc]init]; self.navigationController.navigationBar.barTintColor=[UIColor blueColor];//导航背景色[self loadData];
[self headview];
[self.view addSubview:self.table];
}
-(UITableView *)table{
if(!_table){
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_table.delegate = self;
_table.dataSource = self;
}
return _table;
}
-(void)headview
{
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen ].bounds.size.width, 180)];
UIImageView *imageI=[[UIImageView alloc]initWithFrame:CGRectMake(0, 30, [UIScreen mainScreen ].bounds.size.width, 150)];
imageI.image=[UIImage imageNamed:@"Image"];
[view1 addSubview:imageI];
self.table.tableHeaderView=view1;
}
-(void)loadData{
//显示加载栏
// [SVProgressHUD showWithStatus:@"加载中"];
//GET 请求
//创建网络路径
NSURL *url = [NSURL URLWithString: @"http://127.0.0.1/PengYou.json"];
//创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//获取会话对象
NSURLSession *session = [NSURLSession sharedSession];
//根据会话对象 创建一个task任务
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//对服务器返回的数据进行处理
if (!error) {
NSLog(@"data :%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:1 error:nil];
NSArray *arr = dic[@"result"];
for (NSDictionary *di in arr) {
Model *model = [[Model alloc] init];
[model setValuesForKeysWithDictionary:di];
[self.dataArr addObject:model];
dispatch_async(dispatch_get_main_queue(), ^{
[self.table reloadData];
[SVProgressHUD dismiss];
});
}
}else{
NSLog(@"请求失败 error :%@",error);
}
}];
//执行任务
[sessionTask resume];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arrI2=@[@"Image-5",@"Image-1",@"Image-2",@"Image-3",@"Image-4",@"Image-5"];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
Model *model = self.dataArr[indexPath.row];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imageV]]];
cell.textLabel.text=[NSString stringWithFormat:@"%@",model.TeLabel];
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",model.DeLabel];
return cell;
}
-(void)click
{
//跳转到第二个控制器
fiveViewController *secVc = [fiveViewController new];
[self.navigationController pushViewController:secVc animated:YES];
[self headview];
}
@end