#import “ViewController.h”
#import “AFNetworking/AFNetworking.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableDictionary *_dic;
UITableView *_tbv;
}
@end
#define TEST_URL @“http://127.0.0.1/1608E.json”
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];_tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_tbv.delegate = self;
_tbv.dataSource = self;[self.view addSubview:_tbv];
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->_tbv reloadData];
}];[task resume];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return _dic.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{NSString *str1 = [_dic.allKeys objectAtIndex:section];
return [[_dic objectForKey:str1]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str = @“cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if(!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
} NSString *str1 = [_dic.allKeys objectAtIndex:indexPath.section];
NSString *str2 = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“img”];
cell.textLabel.text = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@“img”];
NSURL *imgUrl = [NSURL URLWithString:str2];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
cell.imageView.image = [[UIImage alloc]initWithData:imgData];
cell.textLabel.text = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“name”];
cell.textLabel.text = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“like”];
return cell;
}