今天我们来做个解析加收藏的功能
我们用在pods中导入
target '项目名称' do
pod 'AFNetworking'
pod 'MBProgressHUD'
end
当然第一步记得开网,
添加FMDB和MJExtension第三方
在AppDelegate.h创建导航栏
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
在ViewController.m里写:
添加的头文件:
import "MJExtension.h"
import "Model.h"
import "shoucangViewController.h"
import "xiangqingViewController.h"
import "AFNetworking.h"
import "tiaoViewController.h"
import "MBProgressHUD.h"
遵守表格协议
创建表格
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *array;
在viewDidLoad里
self.navigationItem.title = @"title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
[self jiexi];
[self.view addSubview:self.tab];
//////////
-(void)showMBHudWithMessage:(NSString *)msg
{
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:self.view];
//设置文本的提示样式
hud.mode=MBProgressHUDModeIndeterminate;
//自动从父视图中移除
hud.removeFromSuperViewOnHide=YES;
hud.label.text=msg;
[self.view addSubview:hud];
[hud showAnimated:YES];
[hud hideAnimated:YES afterDelay:3.0];
}
-(void)jiexi{
//显示一个等待控制器
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:self.view];
hud.removeFromSuperViewOnHide=YES;
[self.view addSubview:hud];
[hud showAnimated:YES];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb&type=top" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
self.array = [Model mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"data"]];
[hud hideAnimated:YES];
[self.tab reloadData];
[self showMBHudWithMessage:@"加载成功"];
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(UITableView *)tab{
if (!_tab) {
_tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tab.dataSource = self;
_tab.delegate = self;
}
return _tab;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
Model *md = self.array[indexPath.row];
cell.textLabel.text = md.title;
cell.detailTextLabel.text = md.category;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
tiaoViewController *tiao = [[tiaoViewController alloc]init];
Model *ml = self.array[indexPath.row];
tiao.mo = ml;
[self.navigationController pushViewController:tiao animated:YES];
}
-(void)right{
shoucangViewController *shoucang = [[shoucangViewController alloc]init];
[self.navigationController pushViewController:shoucang animated:YES];
}
创建datamanager
在datamanager.h里
+(datamanager *)dataModel;
-(void)zengModel:(id)zeng;
-(void)shanModel:(int)shan;
-(id)chaModel;
在datamanager.m里
import "Model.h"
import "FMDB.h"
static datamanager *_defaulthandle = nil;
@interface datamanager()
@property(nonatomic,strong)FMDatabase *fMDB;
@end
@implementation datamanager
+(datamanager *)dataModel{
if (_defaulthandle ==nil) {
_defaulthandle = [datamanager new];
}
return _defaulthandle;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!_defaulthandle) {
_defaulthandle = [super allocWithZone:zone];
}
return _defaulthandle;
}
-(FMDatabase *)fMDB{
if (_fMDB ==nil)
{
NSString *path =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"moview.sqlite"];
NSLog(@"path==%@",path);
_fMDB =[FMDatabase databaseWithPath:path];
[self initTable];
}
return _fMDB;
}
-(void)initTable{
[self.fMDB open];
[self.fMDB executeUpdate:@"CREATE TABLE movie (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,author_name TEXT, url,TEXT)"];
[self.fMDB close];
}
-(void)zengModel:(Model *)zeng{
[self.fMDB open];
[self.fMDB executeUpdateWithFormat:@"insert into movie (title,url) values (%@,%@)",zeng.title,zeng.url];
NSLog(@"收藏成功");
[self.fMDB close];
}
-(void)shanModel:(int)shan{
[self.fMDB open];
[self.fMDB executeUpdateWithFormat:@"DELETE FROM movie WHERE id = %d",shan];
[self.fMDB close];
}
-(id)chaModel{
[self.fMDB open];
NSMutableArray *arr = [[NSMutableArray alloc]init];
FMResultSet *res = [self.fMDB executeQuery:@"SELECT * FROM movie"];
while ([res next]) {
Model *md = [[Model alloc]init];
[arr addObject:md];
md.ID = [res intForColumnIndex:0];
md.title = [res stringForColumnIndex:1];
md.url = [res stringForColumnIndex:2];
}
[self.fMDB close];
return [arr copy ];
}
创建Model
在Model.h里
@property(nonatomic,strong)NSString title,url,*category;
@property(nonatomic,assign)int *ID;
创建tiaoViewcontroller
在tiaoViewcontroller.h里
import
import "Model.h"
@interface tiaoViewController : UIViewController
@property(nonatomic,strong)Model *mo;
@end
在tiaoViewcontroller.m里
import "datamanager.h"
@interface tiaoViewController ()
@property(nonatomic,strong)UIWebView *web;
@end
@implementation tiaoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"美文";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = [NSURL URLWithString:self.mo.url];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self.web loadRequest:req];
[self.view addSubview:self.web];
}
-(void)right{
[[datamanager dataModel]zengModel:self.mo];
}
创建shoucangViewController
在shoucangViewController.m里:
import "Model.h"
import "xiangqingViewController.h"
import "datamanager.h"
@interface shoucangViewController ()
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *array;
@end
@implementation shoucangViewController
-(void)viewDidAppear:(BOOL)animated{
self.array = [[datamanager dataModel]chaModel];
[self.tab reloadData];
}
-
(void)viewDidLoad {
[super viewDidLoad];[self.view addSubview:self.tab];
}
-(UITableView *)tab{
if (!_tab) {
_tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tab.dataSource =self;
_tab.delegate = self;
}
return _tab;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
}
_tab.rowHeight = 100;
Model *model = self.array[indexPath.row];
cell.textLabel.text = model.title;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
xiangqingViewController *xiangqing = [[xiangqingViewController alloc]init];
Model *mode = self.array[indexPath.row];
xiangqing.mod = mode;
[self.navigationController pushViewController:xiangqing animated:YES];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
Model *ml = self.array[indexPath.row];
[[datamanager dataModel]shanModel:ml.ID];
self.array = [[datamanager dataModel]chaModel];
[self.tab reloadData];
}
创建xiangqingViewController
在.h里:
import
import "Model.h"
@interface xiangqingViewController : UIViewController
@property(nonatomic,strong)Model *mod;
@end
在xiangqingViewController.m里
import "datamanager.h"
@interface xiangqingViewController ()
@property(nonatomic,strong)UIWebView *web;
@end
@implementation xiangqingViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"美文详情";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(Clcik)];
self.web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = [NSURL URLWithString:self.mod.url];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[self.web loadRequest:req];
[self.view addSubview:self.web];
}
-(void)Clcik{
[[datamanager dataModel]zengModel:self.mod];
}