本地请求数据NSURLSession--- json 双表关联

业务处理层

#import "Load.h"

@implementation Load

static Load *ldl=nil;

//设置单例

+(instancetype)danli;

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

ldl=[[Load alloc]init];

});

return ldl;

}

+(instancetype)allocWithZone:(struct _NSZone *)zone

{

if (ldl==nil)

{

ldl=[super allocWithZone:zone];

}

return ldl;

}

-(id)copy

{

return self;

}

-(id)mutableCopy

{

return self;

}

-(void)get:(NSString *)urls file:(NSString*)fNa;

{

NSURL *url=[NSURL URLWithString:urls];

NSURLSession *see=[NSURLSession sharedSession];

NSURLSessionDataTask *tas=[see dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

dispatch_async(dispatch_get_main_queue(), ^{

if ([fNa isEqualToString:@"oneN"])

{

//发送通知

[[NSNotificationCenter defaultCenter]postNotificationName:@"mo" object:arr];

}

else

{

[[NSNotificationCenter defaultCenter]postNotificationName:@"se" object:arr];

}

});

}];

[tas resume];

}

主图

#import "ViewController.h"#import "Load.h"#import "AaaViewController.h"@interface ViewController (){

NSArray *arr;

UITableView *tab;

}

#define MAC @"http://127.0.0.1/Move.json"

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//注册通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getm:) name:@"mo" object:nil];

//调用业务管理层

[[Load danli]get:MAC file:@"oneN"];

tab=[[UITableView alloc]initWithFrame:self.view.frame];

tab.delegate=self;

tab.dataSource=self;

[self.view addSubview:tab];

}

-(void)getm:(NSNotification*)not

{

arr=not.object;

[tab reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{

return arr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *str=@"cell";

UITableViewCell *cell=[tab dequeueReusableCellWithIdentifier:str];

if (cell==nil)

{

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];

}

cell.textLabel.text=[[arr objectAtIndex:indexPath.row]objectForKey:@"name"];

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

AaaViewController *aa=[[AaaViewController alloc]init];

//传值

aa.seq=[arr objectAtIndex:indexPath.row];

[self presentModalViewController:aa animated:YES];

}

第二个图.h

@property(nonatomic,strong)NSDictionary *seq;

.m

#import "AaaViewController.h"

#import "Load.h"

@interface AaaViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *vi;

@property (weak, nonatomic) IBOutlet UILabel *lab;

@property (weak, nonatomic) IBOutlet UILabel *lab1;

@property (weak, nonatomic) IBOutlet UILabel *lab2;

#define MAC @"http://127.0.0.1/Model.json"

@end

@implementation AaaViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 注册通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getm:) name:@"mo" object:nil];

//调用业务管理层

[[Load danli]get:MAC file:@"oneN"];

}

-(void)getm:(NSNotification*)not

{

NSArray *arr=not.object;

for (NSDictionary *mess in arr)

{

if ([[mess objectForKey:@"id"] isEqualToString:[self.seq objectForKey:@"id"]])

{

//获取图片

NSData *dat=[NSData dataWithContentsOfURL:[NSURL URLWithString:[mess objectForKey:@"img"]]];

self.vi.image = [[UIImage alloc]initWithData:dat];

self.lab.text = [self.seq objectForKey:@"name"];

self.lab1.text = [mess objectForKey:@"start"];

self.lab2.text = [mess objectForKey:@"price"];

}

}

}

你可能感兴趣的:(本地请求数据NSURLSession--- json 双表关联)