重要

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值