ViewController.m里面
#import “ViewController.h”
#import “TableViewCell.h”
#import “DataModel.h”
#import <AFNetworking.h>
#import “twoViewController.h”
#import “oneViewController.h”
#import “Model.h”
#import “MBProgressHUD.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *DataSource;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tab.delegate=self;
self.tab.dataSource=self;self.navigationItem.title=@“好好学习”;
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“收藏界面” style:UIBarButtonItemStyleDone target:self action:@selector(click)];[self.view addSubview:self.tab];
[self.tab registerNib:[UINib nibWithNibName:@“TableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];
self.DataSource=[[NSMutableArray alloc]init];
[self AFN];
}
// 显示提示文本
-(void)showMBHudWithMessage:(NSString *)msg{
MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
// 设置文本的提示样式
hud.mode = MBProgressHUDModeIndeterminate;
// 自动从父视图移除
hud.removeFromSuperViewOnHide = YES;
hud.labelText = msg;
[self.view addSubview:hud];
[hud show:YES];
[hud hide:YES afterDelay:2.0];
}
-(void)AFN{
// 显示一个等待指示器
MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
hud.removeFromSuperViewOnHide = YES;
[self.view addSubview:hud];
[hud show:YES];
AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
manger.responseSerializer=[AFHTTPResponseSerializer serializer];
[manger GET:@"http://c.m.163.com/nc/article/list/T1348648517839/0-20.html" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
NSDictionary *arr=[dic objectForKey:@"T1348648517839"];
for (NSDictionary *dic in arr) {
DataModel *model=[DataModel new];
[model setValuesForKeysWithDictionary:dic];
[self.DataSource addObject:model];
// 隐藏等待指示器
[hud hide:YES];
NSLog(@"%@",self.DataSource);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self showMBHudWithMessage:@"加载成功"];
[self.tab reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.DataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *oj=@"cell";
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
if (cell==nil) {
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:oj];
}
DataModel *model=self.DataSource[indexPath.row];
self.tab.rowHeight=150;
[cell loadData:model];
// cell.zhu.text=model.title;
//
// cell.fu.text=model.mtime;
//
// cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgsrc]]];
//
return cell;
}
-(void)click{
oneViewController *one=[oneViewController new];
[self.navigationController pushViewController:one animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
twoViewController *two=[twoViewController new];
two.str=self.DataSource[indexPath.row];
[self.navigationController pushViewController:two animated:YES];
}
@end
oneViewController.h
#import <UIKit/UIKit.h>
#import “DataModel.h”
NS_ASSUME_NONNULL_BEGIN
@interface oneViewController : UIViewController
@property(nonatomic,strong)DataModel *mode;
@end
NS_ASSUME_NONNULL_END
oneViewController.m
#import “oneViewController.h”
#import “DataModel.h”
#import “Model.h”
#import “TableViewCell.h”
@interface oneViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *arr;
@end
@implementation oneViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.self.navigationItem.title=@“收藏列表”;
self.tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tab.delegate=self;
self.tab.dataSource=self;[self.tab registerNib:[UINib nibWithNibName:@“TableViewCell” bundle:nil] forCellReuseIdentifier:@“cell”];
[self.view addSubview:self.tab];
}
-(void)viewWillAppear:(BOOL)animated{
[[Model initData]initSql];
//把从数据查询到的数据加载到arr
self.arr=[[Model initData]inquireArr];
}
//刷新表格
[self.tab reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *oj=@"cell";
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
[cell loadData:self.arr[indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[[Model initData]initSql];
//首先确认主键ID
[[Model initData]deletData:[self.arr[indexPath.row]classID]];
//确认完主键ID删除数据
[self.arr removeObject:self.arr[indexPath.row]];
[self.tab reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150;
}
@end
twoViewController.h
#import <UIKit/UIKit.h>
#import “DataModel.h”
NS_ASSUME_NONNULL_BEGIN
@interface twoViewController : UIViewController
@property(nonatomic,strong)DataModel *str;
@end
NS_ASSUME_NONNULL_END
twoViewController.m
#import “twoViewController.h”
#import <WebKit/WebKit.h>
#import “Model.h”
@interface twoViewController ()
@property(nonatomic,strong)WKWebView *webview;
@end
@implementation twoViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.title=@“网页视图”;
self.webview=[[WKWebView alloc]initWithFrame:self.view.bounds];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“收藏” style:UIBarButtonItemStyleDone target:self action:@selector(click)];
//
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.str.url]]]];
[self.view addSubview:self.webview];
}
-(void)click{
[[Model initData]initSql];
[[Model initData]addData:self.str];
}
@end