json文件(本地)
http://127.0.0.1/pengyou.json
vc 导航和分页按钮
#import "ViewController.h"
#import "oneViewController.h"
#import "twoViewController.h"
#import "threeViewController.h"
#import "fourViewController.h"
#import "fiveViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController*one=[[UINavigationController alloc] initWithRootViewController:[oneViewController new]];
one.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
UINavigationController*two=[[UINavigationController alloc] initWithRootViewController:[twoViewController new]];
two.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
UINavigationController*three=[[UINavigationController alloc] initWithRootViewController:[threeViewController new]];
three.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
UINavigationController*four=[[UINavigationController alloc] initWithRootViewController:[fourViewController new]];
four.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
UINavigationController*five=[[UINavigationController alloc] initWithRootViewController:[fiveViewController new]];
five.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:[UIImage imageNamed:@"tabbar_contactsHL"]];
self.tabBar.barTintColor=[UIColor whiteColor];
// self.navigationController.tabBarController.selectedIndex=3;
self.viewControllers=@[one,two,three,four,five];
// Do any additional setup after loading the view, typically from a nib.
[self setSelectedIndex:3];
}
自定义cell 继承与UITableViewCell
cell.h声明属性
@property(nonatomic,strong)UILabel*lab;
@property(nonatomic,strong)UILabel*lab2;
@property(nonatomic,strong)UIImageView*img;
.m里
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self addSubview:self.img];
[self addSubview:self.lab];
[self addSubview:self.lab2];
}
return self;
}
-(UIImage *)img{
if (!_img) {
_img=[[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 50, 50)];
}
return _img;
}
-(UILabel *)lab{
if (!_lab) {
_lab=[[UILabel alloc] initWithFrame:CGRectMake(70, 5, 150, 50)];
_lab.font=[UIFont systemFontOfSize:18];
}
return _lab;
}
-(UILabel *)lab2{
if (!_lab2) {
_lab2=[[UILabel alloc] initWithFrame:CGRectMake(70, 30, 500, 50)];
_lab2.font=[UIFont systemFontOfSize:15];
_lab2.textColor=[UIColor grayColor];
}
return _lab2;
}
vc
four vc
导入第三方文件
pod 'AFNetworking'
pod 'SVProgressHUD'
vc里所有界面加解析
#import "fourViewController.h"
#import "eeTableViewCell.h"
#import <SVProgressHUD.h>
#import <AFNetworking.h>
#import "model.h"
@interface fourViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView*tab;
}
@property(nonatomic,strong)NSMutableArray*dataSoure;
@end
@implementation fourViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSoure=[[NSMutableArray alloc] init];
self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"朋友" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
UIView*vie=[[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 50)];
NSArray*arr=@[@"tabbar_me",@"tabbar_mainframe",@"tabbar_discover"];
for (int i=0; i<3; i++) {
UIButton*btn=[[UIButton alloc] initWithFrame:CGRectMake(200+i*50, 0, 50, 50)];
[btn setImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
[vie addSubview:btn];
}
self.navigationItem.titleView=vie;
tab=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tab.delegate=self;
tab.dataSource=self;
[self.view addSubview:tab];
[self tableHeadview];
[self loadData];
}
-(void)loadData{
//显示加载栏
[SVProgressHUD showWithStatus:@"加载中"];
// 解析数据
// data请求到的东西
[[[NSURLSession sharedSession] dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1/pengyou.json"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 如果错误或者为空 报错
if (error&&data==nil) {
NSLog(@"数据请求错误");
}
// 把解析到的东西给字典
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:1 error:nil];
// 把数据里的KEY给一个数组arr
NSArray *arr = dic[@"dict"];
// 把arr里的所有的key放在字典里
for (NSDictionary *di in arr) {
// 新建一个model
model *ome=[[model alloc] init];
// 把得到的value给model 通过model里的key获取相应的value
[ome setValuesForKeysWithDictionary:di];
// 把模型里的东西给数据源
[self.dataSoure addObject:ome];
dispatch_async(dispatch_get_main_queue(), ^{
[self->tab reloadData];
[SVProgressHUD dismiss];
});
}
}]resume];
}
-(void)tableHeadview{
UIView*vie=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 30, 50, 50) ];
[btn setImage:[UIImage imageNamed:@"home_edition_tao"] forState:UIControlStateNormal];
[vie addSubview:btn];
UIButton*btn1=[[UIButton alloc]initWithFrame:CGRectMake(160, 30, 50, 50) ];
[btn1 setImage:[UIImage imageNamed:@"home_edition_tao"] forState:UIControlStateNormal];
[vie addSubview:btn1];
UIButton*btn2=[[UIButton alloc]initWithFrame:CGRectMake(270, 30, 50, 50) ];
[btn2 setImage:[UIImage imageNamed:@"home_edition_tao"] forState:UIControlStateNormal];
[vie addSubview:btn2];
for (int i=0; i<3; i++) {
NSArray*arr=@[@"阿达禁毒",@"大家拿到今",@"大技能挖"];
NSArray*arr1=@[@"石达川",@"好美",@"闪电发"];
UILabel*lab=[[UILabel alloc] initWithFrame:CGRectMake(50+40*i+70*i, 50, 100, 100)];
lab.text=arr[i];
lab.font=[UIFont systemFontOfSize:13];
[vie addSubview:lab];
UILabel*lab1=[[UILabel alloc]initWithFrame:CGRectMake(50+40*i+70*i, 80, 100, 100)];
lab1.text=arr1[i];
lab1.font=[UIFont systemFontOfSize:13];
lab1.textColor=[UIColor grayColor];
[vie addSubview:lab1];
}
tab.tableHeaderView=vie;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSoure.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
eeTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell=[[eeTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
tab.rowHeight=100;
model *ome = self.dataSoure[indexPath.row];
cell.img.image = [UIImage imageNamed:ome.imageV];
cell.lab.text = [NSString stringWithFormat:@"%@",ome.nameText];
// cell.lab.text=[model.nameText];
cell.lab2.text = [NSString stringWithFormat:@"%@",ome.detailText];
return cell;
}
@end