1.导入AFNetworking
2.更改Info.plist文件
注意:文件样式,用MVC
3.创建MyTableViewCell继承于UITableViewCell
在MyTableViewCell.h中添加:
@property(nonatomic,strong)UILabel *label;
在MyTableViewCell.m中:
#import “MyTableViewCell.h”
@implementation MyTableViewCell
-(void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self addSubview:self.label];
}
return self;
}
-(UILabel *)label{
if (!_label) {
self.label=[[UILabel alloc] initWithFrame:CGRectMake(200, 20, 300, 40)];
self.label.font=[UIFont systemFontOfSize:18];
self.label.numberOfLines=0;
}
return _label;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
4.分别创建oneViewController,twoViewController,threeViewController继承于UIViewController
在twoViewController.m中:
#import “twoViewController.h”
#import “threeViewController.h”
#import “AFNetworking.h”
#import “MyTableViewCell.h”
#define TEST_URL @“http://192.168.100.7/yk.json”
@interface twoViewController ()<UITableViewDelegate,UITableViewDataSource>{
//分段控制器
UISegmentedControl *seg;
UITableView *tbv;
NSDictionary *_dic;
UITableView *tab;
}
@end
@implementation twoViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.title=@"资讯";
tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
tab.delegate = self;
tab.dataSource = self;
[tab registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
[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";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strc];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strc];
}
tab.rowHeight=100;
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.label.text = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];
return cell;
}
@end
5.在ViewController.m中:
#import “ViewController.h”
#import “oneViewController.h”
#import “twoViewController.h”
#import “threeViewController.h”
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
oneViewController *one=[oneViewController new];
UINavigationController *onenav=[[UINavigationController alloc] initWithRootViewController:one];
onenav.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"报告" image:[UIImage imageNamed:@"购物车"] selectedImage:[UIImage imageNamed:@"购物车"]];
twoViewController *two=[twoViewController new];
UINavigationController *twonav=[[UINavigationController alloc] initWithRootViewController:two];
twonav.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"通讯录"] selectedImage:[UIImage imageNamed:@"通讯录"]];
threeViewController *three=[threeViewController new];
UINavigationController *threenav=[[UINavigationController alloc] initWithRootViewController:three];
threenav.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"我的"] selectedImage:[UIImage imageNamed:@"我的"]];
self.viewControllers=@[onenav,twonav,threenav];
}
@end