导入
#import <SVProgressHUD.h>
#import <AFNetworking.h>
json数据
{“dict”:
[{“nameText”:“服务提醒(含支付助手)”,“detailText”:“今天你累积的免费医疗金未领取”,“imageV”:“1.jpg”},
{“nameText”:“优酷VIP会员”,“detailText”:“被玩坏了的《爱情公寓》暑期档最具争议的电影”,“imageV”:“2.jpg”},
{“nameText”:“手机充值”,“detailText”:“全国流量不限量!网上冲浪,任你享!”,“imageV”:“3.jpg”},{“nameText”:“芝麻信用”,“detailText”:“远离甲醛,你的芝麻分也许能帮上忙”,“imageV”:“4.jpg”},
{“nameText”:“高磊”,“detailText”:"[转账]转账30元",“imageV”:“5.jpg”},
{“nameText”:“gynan”,“detailText”:“?”,“imageV”:“6.jpg”}
]
}
model
@property(nonatomic,copy)NSString *nameText;//标题
@property(nonatomic,copy)NSString *detailText;//标题
@property(nonatomic,copy)NSString *imageV;//标题
界面
//
// fourViewController.m
// 周考练习2
//
// Created by 毅先森 on 2018/11/9.
// Copyright © 2018 刘毅. All rights reserved.
//
#import "fourViewController.h"
#import "oneViewController.h"
#import <SVProgressHUD.h>
#import <AFNetworking.h>
#import "Model.h"
#define URL @"http://127.0.0.1/pengyou.json"
@interface fourViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end
@implementation fourViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *uiv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 44)];
title.text = @"朋友";
title.textAlignment = NSTextAlignmentLeft;
title.textColor = [UIColor whiteColor];
[uiv addSubview:title];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(270, 0, 40, 40)];
[btn setImage:[UIImage imageNamed:@"tabbar_mainframeHL"] forState:UIControlStateNormal];
[uiv addSubview:btn];
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(310, 0, 40, 40)];
[btn2 setImage:[UIImage imageNamed:@"tabbar_mainframeHL"] forState:UIControlStateNormal];
[uiv addSubview:btn2];
UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(350, 0, 40, 40)];
[btn3 setImage:[UIImage imageNamed:@"tabbar_mainframeHL"] forState:UIControlStateNormal];
[uiv addSubview:btn3];
self.navigationItem.titleView = uiv;
self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
self.dataArr = [[NSMutableArray alloc]init];
[self.view addSubview:self.tbv];
[self tbvHeadView];
[self loadData];
}
-(void)loadData{
//显示加载栏
[SVProgressHUD showWithStatus:@"加载中"];
//GET 请求
//创建网络路径
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/pengyou.json"];
//创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//获取会话对象
NSURLSession *session = [NSURLSession sharedSession];
//根据会话对象 创建一个task任务
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//对服务器返回的数据进行处理
if (data &&error == nil) {
NSLog(@"data :%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:1 error:nil];
NSArray *arr = dic[@"dict"];
for (NSDictionary *di in arr) {
Model *model = [[Model alloc] init];
[model setValuesForKeysWithDictionary:di];
[self.dataArr addObject:model];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tbv reloadData];
[SVProgressHUD dismiss];
});
}
}else{
NSLog(@"请求失败 error :%@",error);
}
}];
//执行任务
[sessionTask resume];
}
-(void)tbvHeadView{
UIView *uiv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
NSArray *imgvArr = @[@"1",@"2",@"3"];
NSArray *textArr = @[@"生活号",@"小程序",@"生活圈"];
NSArray *detailArr = @[@"一个神奇的杯子",@"一个神奇的杯子",@"一个神奇的杯子"];
for (int i=0; i<3; i++) {
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3*i+29, 10, 80, 80)];
[btn setImage:[UIImage imageNamed:imgvArr[i]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[uiv addSubview:btn];
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3*i, 90, 138, 30)];
lb.text = textArr[i];
lb.textAlignment = NSTextAlignmentCenter;
[uiv addSubview:lb];
UILabel *lb2 = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3*i, 120, 138, 30)];
lb2.text = detailArr[i];
lb2.textAlignment = NSTextAlignmentCenter;
[uiv addSubview:lb2];
}
self.tbv.tableHeaderView = uiv;
}
-(void)click{
oneViewController *one = [oneViewController new];
[self.navigationController pushViewController:one animated:YES];
}
- (UITableView *)tbv{
if (!_tbv) {
_tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_tbv.delegate = self;
_tbv.dataSource = self;
}
return _tbv;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// return self.dataArr.count;
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//静态字符串标识
static NSString *str = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
Model *model = self.dataArr[indexPath.row];
cell.imageView.image = [UIImage imageNamed:model.imageV];
cell.textLabel.text = [NSString stringWithFormat:@"%@",model.nameText];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",model.detailText];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_tbv deselectRowAtIndexPath:indexPath animated:YES];
}
@end