#import “AFNetworking/AFNetworking.h”
#define TEST_URL @“http://192.168.0.113/25yk.json”
@interface OneViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSDictionary *_dic;
UITableView *tab;
}
@end
@implementation OneViewController
-(void)viewDidLoad {
[super viewDidLoad];
tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
tab.delegate = self;
tab.dataSource = self;
[self.view addSubview:tab];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
self->_dic = responseObject;
[self->tab reloadData];
}];
[task resume];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dic.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *str = [_dic.allKeys objectAtIndex:section];
return [[_dic objectForKey:str]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *strc = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strc];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strc];
}
NSString *str = [_dic.allKeys objectAtIndex:indexPath.section];
NSString *imgstr = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL *urlimg = [NSURL URLWithString:imgstr];
NSData *data = [NSData dataWithContentsOfURL:urlimg];
cell.imageView.image = [[UIImage alloc]initWithData:data];
cell.textLabel.text = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"like"];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_dic.allKeys objectAtIndex:section];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
MyViewController *my = [[MyViewController alloc]init];
//获取某个分区
NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];
//属性传值 字典
my.dic = [[_dic objectForKey:key]objectAtIndex:indexPath.row
];
[self presentViewController:my animated:YES completion:nil];
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end