AFNetworking網(wǎng)絡(luò)請(qǐng)求

首先添加AFNetworking第三方,(用的xib)

屏幕快照 2018-11-21 下午3.11.14.png

最先改info.plist文件:
App Transport Security Settings
Allow Arbitrary Loads 是yes


屏幕快照 2018-11-23 上午12.10.33.png

ViewController.m里

import "zhuanViewController.h"

import "AFNetworking/AFNetworking.h"

import "zidTableViewCell.h"

@interface sanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray *arr;

@end

  • (void)viewDidLoad {
    [super viewDidLoad];
[self.view addSubview:self.tableview];
[self loadData];

}

-(void)loadData{
AFHTTPSessionManager *manager= [AFHTTPSessionManager manager];
[manager GET:@"http://這里是要請(qǐng)求的網(wǎng)址" parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
self.arr = responseObject;
[self.tableview reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}

//表格的
-(UITableView *)tableview{
if (!_tableview) {
_tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableview.delegate=self;
_tableview.dataSource=self;
[_tableview registerNib:[UINib nibWithNibName:@"zidTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}
return _tableview;
}

//組數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

zidTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NSDictionary *dic = _arr[indexPath.row];
cell.img.image = [UIImage imageWithData:[self ImageWithString:dic[@"cover"]]];
cell.title.text = dic[@"setname"];
NSArray *av= dic[@"pics"];
cell.tupian.text =[NSString stringWithFormat:@"%ld圖",av.count];
cell.lun.text = dic[@"replynum"];
return cell;

}

//點(diǎn)擊跳轉(zhuǎn)的
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
zhuanViewController *zhuan = [[zhuanViewController alloc] init];
self.tabBarController.tabBar.hidden = YES;
[self.navigationController pushViewController:zhuan animated:YES];
self.tabBarController.tabBar.hidden = NO;
NSDictionary *dic = _arr[indexPath.row];
zhuan.arr =dic[@"pics"];
zhuan.string = dic[@"setname"];
zhuan.desc = dic[@"desc"];
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}

-(NSData *)ImageWithString:(NSString *)String{
NSURL *url = [NSURL URLWithString:String];
return [NSData dataWithContentsOfURL:url];
}
//----------------------------

zhuanViewController.h里
@property(nonatomic,copy) NSArray *arr;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,copy )NSString *desc;

zhuan.m

define scr_w self.view.frame.size.width

define scr_h self.view.frame.size.height

@interface zhuanViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scr;
@property(nonatomic,strong)UILabel *lab;
@property(nonatomic,strong)UILabel *lab1;
@property(nonatomic,strong)UILabel *lab2;

@end

  • (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.scr];
    [self.view addSubview:self.lab];
    [self.view addSubview:self.lab1];
    [self.view addSubview:self.lab2];

}

-(UIScrollView *)scr{
if (!_scr) {
_scr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, scr_w, 200)];
_scr.scrollEnabled=YES;
_scr.pagingEnabled = YES;
_scr.bounces = YES;
_scr.backgroundColor = [UIColor blackColor];
_scr.delegate=self;
}
return _scr;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat x = scrollView.contentOffset.x/scr_w;
_lab2.text = [NSString stringWithFormat:@"%d/%ld",(int)x,_arr.count];
}
-(void)setArr:(NSArray *)arr{
_arr = arr;
_scr.contentSize= CGSizeMake(scr_w *arr.count, scr_h);

for ( int i = 0 ; i<arr.count; i++) {
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(i*scr_w, 0, scr_w, 200)];
    [_scr addSubview:image];
    image.image = [UIImage imageWithData:[self ImageWithString:arr[i]]];
}
_lab2.text = [NSString stringWithFormat:@"0/%ld",arr.count];

}
-(void)setString:(NSString *)string{
_string = string;
_lab.text = string;
}
-(void)setDesc:(NSString *)desc{
_desc = desc;
_lab1.text = desc;
}

-(NSData *)ImageWithString:(NSString *)String{
NSURL *url = [NSURL URLWithString:String];
return [NSData dataWithContentsOfURL:url];
}

-(UILabel *)lab{
if (!_lab) {
_lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, scr_w - 50, 30)];
_lab.textColor = [UIColor whiteColor];
}
return _lab;
}
-(UILabel *)lab1{
if (!_lab1) {
_lab1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, scr_w-50, 100)];
_lab1.textColor = [UIColor whiteColor];
_lab1.numberOfLines = 0;
}
return _lab1;
}
-(UILabel *)lab2{
if (!_lab2) {
_lab2 = [[UILabel alloc] initWithFrame:CGRectMake(scr_w-50, 300, 50, 30)];
_lab2.textColor = [UIColor whiteColor];

}
return _lab2;

}

//zidTableViewCell.h里的(xib拖來(lái)的)


屏幕快照 2018-11-21 下午3.25.18.png

//這是xib的樣子


屏幕快照 2018-11-21 下午3.25.28.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市铁材,隨后出現(xiàn)的幾起案子无埃,更是在濱河造成了極大的恐慌,老刑警劉巖昆禽,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卿拴,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡吼旧,警方通過(guò)查閱死者的電腦和手機(jī)凰锡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)圈暗,“玉大人掂为,你說(shuō)我怎么就攤上這事≡贝” “怎么了勇哗?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)寸齐。 經(jīng)常有香客問(wèn)我欲诺,道長(zhǎng),這世上最難降的妖魔是什么渺鹦? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任扰法,我火速辦了婚禮,結(jié)果婚禮上毅厚,老公的妹妹穿的比我還像新娘塞颁。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布祠锣。 她就那樣靜靜地躺著酷窥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伴网。 梳的紋絲不亂的頭發(fā)上蓬推,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音澡腾,去河邊找鬼拳氢。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蛋铆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播放接,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼刺啦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了纠脾?” 一聲冷哼從身側(cè)響起玛瘸,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎苟蹈,沒(méi)想到半個(gè)月后糊渊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡慧脱,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年渺绒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片菱鸥。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡宗兼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出氮采,到底是詐尸還是另有隱情殷绍,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布鹊漠,位于F島的核電站主到,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏躯概。R本人自食惡果不足惜登钥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望楞陷。 院中可真熱鬧怔鳖,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至献幔,卻和暖如春懂傀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蜡感。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工蹬蚁, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人郑兴。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓犀斋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親情连。 傳聞我的和親對(duì)象是個(gè)殘疾皇子叽粹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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