#import@interface DataBase : NSObject
{
NSDictionary *dict;
}
+ (instancetype)shareData;
- (void)getURL;
//
//? DataBase.m
//? 項目一月考技能
//
//? Created by hh on 2017/11/23.
//? Copyright ? 2017年 www.baidu.com. All rights reserved.
//
#import "DataBase.h"
#import "AFNetworking.h"
#import "JSONKit.h"
#define Monthly_Examination @"http://api.jisuapi.com/news/get?channel=頭條&start=0&num=10&appkey=de394933e1a3e2db"
static DataBase *db = nil;
@implementation DataBase
+ (instancetype)shareData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
db = [[DataBase alloc]init];
});
return db;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!db)
{
db = [super allocWithZone:zone];
}
return db;
}
- (id)mutableCopy
{
return self;
}
- (id)copy
{
return self;
}
- (void)getURL
{
dict = [[NSDictionary alloc]init];
NSString *strURl = [Monthly_Examination stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
manger.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
[manger GET:strURl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"成功");
dict = [responseObject objectFromJSONData];
[[NSNotificationCenter defaultCenter]postNotificationName:@"huxiaobo" object:dict];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"失敗");
}];
}
@end
//// FirstViewController.m// 項目一月考技能//// Created by hh on 2017/11/23.// Copyright ? 2017年 www.baidu.com. All rights reserved.//#import "FirstViewController.h"#import "DataBase.h"#import "FffffViewController.h"@interface FirstViewController (){
// 全局變量
NSDictionary *dicts;
NSArray *arrays;
}
// 屬性? 表格;
@property (nonatomic,strong)UITableView *tables;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 頁面的背景顏色及導(dǎo)航欄的標(biāo)題、背景顏色
[self setBackgroundColor];
// 類方法將DataBase類里的getURL正此控制器實現(xiàn)以及通知傳值(接收)
[self getUrlwithdata];
// 表格的創(chuàng)建
[self tables];
}
#pragma mark - 頁面的背景顏色及導(dǎo)航欄的標(biāo)題、背景顏色;
- (void)setBackgroundColor
{
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"頭條";
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:20 weight:1.5],
NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
#pragma mark - 類方法將DataBase類里的getURL正此控制器實現(xiàn)以及通知傳值(接收);
- (void)getUrlwithdata
{
[[DataBase shareData]getURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(TransferData:) name:@"huxiaobo" object:nil];
}
#pragma mark - 表格的創(chuàng)建 位置及大小 和設(shè)置代理;
- (UITableView *)tables
{
if (!_tables)
{
_tables = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tables.delegate =self;
_tables.dataSource = self;
[self.view addSubview:_tables];
}
return _tables;
}
#pragma mark - 表格的代理及數(shù)據(jù)源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrays.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellS = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellS];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellS];
}
cell.textLabel.text = [[arrays objectAtIndex:indexPath.row] objectForKey:@"title"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[arrays objectAtIndex:2] objectForKey:@"pic"]]];
//image與data的相互轉(zhuǎn)換
UIImage *image = [UIImage imageWithData:data];
//配置圖片到imageView上
cell.imageView.image = image;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FffffViewController *fff = [[FffffViewController alloc]init];
fff.WebUrl = [[arrays objectAtIndex:indexPath.row] objectForKey:@"weburl"];
[self.navigationController pushViewController:fff animated:YES];
}
#pragma mark - 通知傳值附加的事件;
- (void)TransferData:(NSNotification *)notifi
{
arrays = [[NSArray alloc]init];
dicts = [[NSDictionary alloc]init];
dicts = notifi.object;
arrays = [[dicts objectForKey:@"result"] objectForKey:@"list"];
NSLog(@"arrays:::::%@",arrays);
[self.tables reloadData];
}
@end
//// FffffViewController.h// 項目一月考技能//// Created by hh on 2017/11/23.// Copyright ? 2017年 www.baidu.com. All rights reserved.//#import@interface FffffViewController : UIViewController
@property (nonatomic,strong)NSString *WebUrl;
@end
//
//? FffffViewController.m
//? 項目一月考技能
//
//? Created by hh on 2017/11/23.
//? Copyright ? 2017年 www.baidu.com. All rights reserved.
//
#import "FffffViewController.h"
@interface FffffViewController ()
@end
@implementation FffffViewController
- (void)viewDidLoad {
[super viewDidLoad];
//頁面背景顏色
self.view.backgroundColor = [UIColor whiteColor];
//網(wǎng)頁視圖大小和位置
UIWebView* webView = [[UIWebView alloc]initWithFrame:self.view.frame];
//為url添加內(nèi)容
NSURL *url = [NSURL URLWithString:self.WebUrl];//創(chuàng)建URL
//請求
NSURLRequest* request = [NSURLRequest requestWithURL:url];
//加載
[webView loadRequest:request];
//將web視圖添加到視圖上
[self.view addSubview:webView];
}
@end