#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
{
NSDictionary *_dic;
}
@property(nonatomic,strong)UITableView *Mytableview;
@end
#define JSON_URL @"http://127.0.0.1/1602C1.json"
#define XML_URL @"http://127.0.0.1/1602C1.xml"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//====二进制解析器======
//初始化AF对象 (注意初始化解析器)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// AFXMLParserResponseSerializer === SAX解析
// AFJSONResponseSerializer === JSON解析 类型(jsonkit)
// AFHTTPResponseSerializer ==二进制解析==NsData类型
//设置解析类型
manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
//get请求 1(字符串地址)2(是否需要传参 {字典})3请求成功后自动回调 4(请求失败自动回调)
[manager GET:JSON_URLparameters:nilsuccess:^(AFHTTPRequestOperation *operation,idresponseObject) {
//=====二进制解析我器 之JSON解析======
// //如果是二进制解析器 则responseObject是NSData类型
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
// NSLog(@"===%@",dic);
//
//
//=====二进制解析我器 之SAX解析======
// NSXMLParser *parser = [[NSXMLParser alloc]initWithData:responseObject];
//
// parser.delegate = self;
//
// [parser parse];
//
//json解析器
//类型转换 将id转换为字典
_dic = (NSDictionary *)responseObject;
NSLog(@"%@",_dic);
[self.Mytableview reloadData];
// //sax解析器
// NSXMLParser *parser = (NSXMLParser *)responseObject;
// parser.delegate = self;
// [parser parse];
//
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[self.view addSubview:self.Mytableview];
}
-(UITableView*)Mytableview
{
if (!_Mytableview)
{
_Mytableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
_Mytableview.delegate=self;
_Mytableview.dataSource=self;
}
return _Mytableview;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return _dic.allKeys.count;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
NSString*key = [_dic.allKeysobjectAtIndex:section];
return [[_dic objectForKey:key]count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*s =@"asdfghj";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:s];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:s];
}
NSString*key = [_dic.allKeysobjectAtIndex:indexPath.section];
cell.textLabel.text = [[[_dic objectForKey:key]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text = [[[_dic objectForKey:key]objectAtIndex:indexPath.row]objectForKey:@"like"];
returncell;
}
@end
//==================================
.h里
#import
@interface BaseViewController : UIViewController
// 创建表格属性
@property (nonatomic,strong) UITableView *table;
// 创建数据数组
@property (nonatomic,strong) NSMutableArray *dataArray;
#pragma mark - 赋值根控制器
- (void)setTabbarVc:(UITabBarController*)tVc;
// 添加表格的方法
- (UITableView*)addTableView;
// 添加内容数组的方法
- (NSMutableArray*)addDataArray;
// 添加导航条左侧图片按钮
- (void)setleftNavgationItem;
// 添加导航条左侧返回按钮
- (void)setLeftBack;
.m里
#import "BaseViewController.h"
@interface BaseViewController ()
{
UITabBarController *_tabbarVc;
}
@end
@implementationBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// // 添加滑动效果
// UIButton *leftItemBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
// // 添加点击事件
// [leftItemBtn addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
// // 设置裁剪
// leftItemBtn.layer.cornerRadius = 20;
// leftItemBtn.layer.masksToBounds = YES;
// // 设置背景图片
// [leftItemBtn setBackgroundImage:[UIImage imageNamed:@"nav_itme_left"] forState:UIControlStateNormal];
//
//// UIImage *leftItemImg = [UIImage imageNamed:@"nav_itme_left"];
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftItemBtn];
}
// 设置表格set方法
- (UITableView*)addTableView{
if(!_table) {
// 初始化
_table = [[UITableView alloc]initWithFrame:SCREEN_BOUNDS style:UITableViewStylePlain];
// 设置代理
_table.delegate=self;
_table.dataSource=self;
// 添加到主视图
[self.viewaddSubview:_table];
}
return _table;
}
// 设置内容set方法
- (NSMutableArray*)addDataArray{
if (!_dataArray) {
_dataArray= [NSMutableArrayarray];
}
return _dataArray;
}
// 设置左侧按钮
- (void)setleftNavgationItem{
// 添加滑动效果
UIButton*leftItemBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,40,40)];
// 添加点击事件
[leftItemBtnaddTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside];
// 设置裁剪
leftItemBtn.layer.cornerRadius=20;
leftItemBtn.layer.masksToBounds=YES;
// 设置背景图片
[leftItemBtnsetBackgroundImage:[UIImage imageNamed:@"nav_itme_left"] forState:UIControlStateNormal];
// UIImage *leftItemImg = [UIImage imageNamed:@"nav_itme_left"];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftItemBtn];
}
#pragma mark - 导航左侧按钮方法
- (void)leftClick:(UIBarButtonItem*)item{
if(_tabbarVc.view.frame.origin.x==0) {
// 添加动画并且zuo'hu
[UIView animateWithDuration:1.0 animations:^{
self->_tabbarVc.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*0.8,0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}];
}else{
// 回到原来的位置
[UIView animateWithDuration:1.0 animations:^{
self->_tabbarVc.view.frame = CGRectMake(0.0,0.0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}];
}
}
#pragma mark - 赋值根控制器
- (void)setTabbarVc:(UITabBarController*)tVc{
_tabbarVc= tVc;
}
// 添加导航条左侧返回按钮
- (void)setLeftBack{
// 添加滑动效果
UIButton*leftItemBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,40,40)];
// 添加点击事件
[leftItemBtnaddTarget:self action:@selector(leftBackClick) forControlEvents:UIControlEventTouchUpInside];
// 设置裁剪
leftItemBtn.layer.cornerRadius=20;
leftItemBtn.layer.masksToBounds=YES;
// 设置背景图片
[leftItemBtnsetBackgroundImage:[UIImage imageNamed:@"login_navigationItem_return"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftItemBtn];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
}
- (void)leftBackClick{
[self dismissViewControllerAnimated:YES completion:nil];
}