網(wǎng)絡(luò)請求

#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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市桐智,隨后出現(xiàn)的幾起案子它呀,更是在濱河造成了極大的恐慌察绷,老刑警劉巖群扶,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哥遮,死亡現(xiàn)場離奇詭異续镇,居然都是意外死亡征绎,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門磨取,熙熙樓的掌柜王于貴愁眉苦臉地迎上來人柿,“玉大人,你說我怎么就攤上這事忙厌≠灬” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵逢净,是天一觀的道長哥放。 經(jīng)常有香客問我,道長爹土,這世上最難降的妖魔是什么甥雕? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮胀茵,結(jié)果婚禮上社露,老公的妹妹穿的比我還像新娘。我一直安慰自己琼娘,他們只是感情好峭弟,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布附鸽。 她就那樣靜靜地躺著,像睡著了一般瞒瘸。 火紅的嫁衣襯著肌膚如雪坷备。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天情臭,我揣著相機與錄音省撑,去河邊找鬼。 笑死俯在,一個胖子當(dāng)著我的面吹牛竟秫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播朝巫,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鸿摇,長吁一口氣:“原來是場噩夢啊……” “哼石景!你這毒婦竟也來了劈猿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤潮孽,失蹤者是張志新(化名)和其女友劉穎揪荣,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體往史,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡仗颈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了椎例。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挨决。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖订歪,靈堂內(nèi)的尸體忽然破棺而出脖祈,到底是詐尸還是另有隱情,我是刑警寧澤刷晋,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布盖高,位于F島的核電站,受9級特大地震影響眼虱,放射性物質(zhì)發(fā)生泄漏喻奥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一捏悬、第九天 我趴在偏房一處隱蔽的房頂上張望撞蚕。 院中可真熱鬧,春花似錦过牙、人聲如沸诈豌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽矫渔。三九已至彤蔽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間庙洼,已是汗流浹背顿痪。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留油够,地道東北人蚁袭。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像石咬,于是被迫代替她去往敵國和親揩悄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容