1.导入AFNetworking第三方框架
2.设置info允许网络请求
3.AppDELEGATA中设置根目录以及导航
4.根据鬼脸提供的数据创建模型类中设置应该有的属性
5.在model.m中的代码如下
6.在第一个界面点击cell跳转到第二个界面的时候一定要设置一个传值的属性
代码如下:
//
// ViewController.m
// 恋爱宝典
//
// Created by Apple on 2018/8/17.
// Copyright © 2018年 chuck. All rights reserved.
//
#import "ViewController.h"
#import "Model.h"
#import "AFHTTPSessionManager.h"
#import "Clickview.h"
#define JSON_URL @"http://d.yixincao.com/interface/getdata.php?act=list&type=nuanwen&page=0 "
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong) UITableView *tableview;
@property(nonatomic , strong) NSMutableArray *datasource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setnav];
[self.view addSubview:self.tableview];
//创建数据请求管理对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//get接口
NSString *utf = [JSON_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
manager.responseSerializer.acceptableContentTypes = [NSSet
setWithObjects:@"application/json", @"text/json", @"text/javascript",
@"text/html", nil];
[manager GET:utf parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%@",downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
for (NSDictionary *dic in responseObject[@"data"]) {
Model *modell = [Model new];
[modell setValuesForKeysWithDictionary:dic];
[self.datasource addObject:modell];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"///////%@",error);
}];
}
-(void)setnav{
self.navigationItem.title = @"读美文";
}
-(UITableView *)tableview{
if (_tableview == nil) {
_tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableview.delegate = self;
_tableview.dataSource = self;
self.datasource = [NSMutableArray new];
_tableview.rowHeight = 150;
}
return _tableview;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.datasource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *str = @"1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
if (self.datasource.count >0) {
Model *model = self.datasource[indexPath.row];
// cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"data_pic.%@",model.pic]];
// cell.imageView.image = [UIImage imageNamed:@"xiaoXi"];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.datasource[indexPath.row]pic]]]];
cell.textLabel.lineBreakMode = UILineBreakModeCharacterWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = model.title;
cell.detailTextLabel.text = model.author;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
Clickview *clickvc = [Clickview new];
clickvc.typeID = [self.datasource[indexPath.row]ID];
[self.navigationController pushViewController:clickvc animated:YES];
}
@end
7.之后在点击cell中的.h中设置传值的属性,在.m中设置新的页面,请求新的数据代码如下:
//
// Clickview.m
// 恋爱宝典
//
// Created by Apple on 2018/8/17.
// Copyright © 2018年 chuck. All rights reserved.
//
#import "Clickview.h"
#import "Model.h"
@interface Clickview ()
@property(nonatomic , strong) UIWebView *web;
@end
@implementation Clickview
- (void)viewDidLoad {
[super viewDidLoad];
//[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:ARTICALDETAILURL,self.model.dataID]]]];
self.web = [[UIWebView alloc]initWithFrame:self.view.frame];
//屏幕适应大小
self.web.scalesPageToFit = YES;
//给路径,出现值
[self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://d.yixincao.com/detailshow.php?id=%@&from=ios",self.typeID]]]];
[self.view addSubview:self.web];
}
@end