表格json解析
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tbv;
NSDictionary *_dic;
}
@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];
NSURL*url = [NSURLURLWithString:TEST_URL];
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
NSURLSessionDataTask*task = [managerdataTaskWithRequest:requestcompletionHandler:^(NSURLResponse*response,idresponseObject,NSError*error) {
self->_dic= responseObject;
[self->tbvreloadData];
}];
[taskresume];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return_dic.count;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
NSString*str = [_dic.allKeysobjectAtIndex:section];
return [[_dic objectForKey:str]count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString*afc =@"123";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:afc];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:afc];
}
NSString*str1 = [_dic.allKeysobjectAtIndex:indexPath.section];
NSString*str2 = [[[_dicobjectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@"img"];
cell.textLabel.text = [[[_dic objectForKey:afc]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL*imgUrl = [NSURLURLWithString:str2];
NSData*imgData = [NSDatadataWithContentsOfURL:imgUrl];
cell.imageView.image= [[UIImagealloc]initWithData:imgData];
cell.textLabel.text= [[[_dicobjectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.textLabel.text= [[[_dicobjectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@"like"];
returncell;
}
@end