AFNetworking网络请求

2018-11-21  本文已影响0人  ViewController_

首先添加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

[self.view addSubview:self.tableview];
[self loadData];

}

-(void)loadData{
AFHTTPSessionManager *manager= [AFHTTPSessionManager manager];
[manager GET:@"//这里是要请求的网址" 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;
}

//组数
-(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;

}

//点击跳转的
-(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

}

-(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拖来的)


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

//这是xib的样子


屏幕快照 2018-11-21 下午3.25.28.png
上一篇下一篇

猜你喜欢

热点阅读