在工程中导入MJExtension FMDB 在pods中导入 target ‘FMDB+MB’ do pod ‘AFNetworking’ pod ‘MBProgressHUD’ @end
先写Datamanager 在.h里
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Datamanager : NSObject +(Datamanager *)datamodel; //增 -(void)zengModel:(id)zeng; //删 -(void)shanModel:(int)shan; //查 -(id)chaModel; @end NS_ASSUME_NONNULL_END在.m里
#import "Datamanager.h" #import "FMDB.h" #import "MJEModel.h" static Datamanager *_defaulthandle =nil; @interface Datamanager () @property(nonatomic,strong)FMDatabase*fMDB; @end @implementation Datamanager //创建单例类 +(Datamanager *)datamodel{ if (!_defaulthandle) { _defaulthandle=[[Datamanager alloc]init]; } 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,category TEXT, url,TEXT)"]; [self.fMDB close]; } //增 -(void)zengModel:(MJEModel *)zeng{ [self.fMDB open]; [self.fMDB executeUpdateWithFormat:@"insert into movie (title,category,url) values (%@,%@,%@)",zeng.title,zeng.category,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 *result=[self.fMDB executeQuery:@"SELECT * FROM movie"]; while ([result next]) { MJEModel *small=[[MJEModel alloc]init]; [arr addObject:small]; small.ID=[result intForColumnIndex:0]; small.title=[result stringForColumnIndex:1]; small.category=[result stringForColumnIndex:2]; small.url=[result stringForColumnIndex:3]; } [self.fMDB close]; return [arr copy]; } @end*在创建的model.h里
@interface MJEModel : NSObject @property(nonatomic , assign)int ID; @property(nonatomic , strong)NSString *title; //@property(nonatomic , strong)NSString *date; @property(nonatomic , strong)NSString *category; @property(nonatomic , strong)NSString *url; @end*在创建的model.m里
#import "MJEModel.h" @implementation MJEModel + (NSDictionary *)modelContainerPropertyGenericClass { return @{ @"data":[MJEModel class] }; }在ViewController.m里
#import "ViewController.h" #import "twoViewController.h" #import "AFNetworking.h" #import "MJExtension.h" #import "AFHTTPSessionManager.h" #import "MJEModel.h" #import "youViewController.h" #import "MBProgressHUD.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView *tbv; @property(nonatomic,strong)NSMutableArray *dataArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.title=@"啦啦啦啦啦啦"; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right)]; [self loadData]; [self.view addSubview:self.tbv]; } -(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)loadData { //显示一个等待控制器 MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:self.view]; hud.removeFromSuperViewOnHide=YES; [self.view addSubview:hud]; [hud showAnimated:YES]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager POST:@"http://v.juhe.cn/toutiao/index?key=67968aeebf1f4e3b6170f7217b6f3cdb&type=top" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { [hud hideAnimated:YES]; NSLog(@"JSON: %@", responseObject); self.dataArray = [MJEModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"data"]]; [self showMBHudWithMessage:@"加载成功"]; //刷新表格 [self.tbv reloadData]; } failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@", error); }]; } -(UITableView *)tbv{ if (!_tbv) { _tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; _tbv.delegate=self; _tbv.dataSource=self; } return _tbv; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]; } _tbv.rowHeight=100; MJEModel *mod=self.dataArray[indexPath.row]; cell.textLabel.text=mod.title; cell.detailTextLabel.text=mod.category; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ twoViewController *two=[twoViewController new]; MJEModel *model=self.dataArray[indexPath.row]; two.StrUrl=model; [self.navigationController pushViewController:two animated:YES]; } -(void)right{ youViewController *right=[youViewController new]; [self.navigationController pushViewController:right animated:YES]; } @end在twoViewController.h
#import "MJEModel.h" NS_ASSUME_NONNULL_BEGIN @interface twoViewController : UIViewController @property(nonatomic,strong)MJEModel *StrUrl; @end跳转后详情界面 在twoViewController.m
#import "twoViewController.h" #import "Datamanager.h" @interface twoViewController () @end @implementation twoViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(right)]; // web 详情y界面 UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; NSURL *url=[NSURL URLWithString:self.StrUrl.url]; NSURLRequest *requst=[NSURLRequest requestWithURL:url]; [web loadRequest:requst]; [self.view addSubview:web]; } //点击收藏按钮 -(void)right{ [[Datamanager datamodel]zengModel:self.StrUrl]; } @end主界面收藏按钮跳转按钮界面 在youViewController.m里
#import "youViewController.h" #import "youxiangqingViewController.h" #import "MJEModel.h" #import "Datamanager.h" #import "twoViewController.h" @interface youViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong)UITableView *table; @property(nonatomic,strong)NSMutableArray *datasource; @end @implementation youViewController //查询 - (void)viewDidAppear:(BOOL)animated{ self.datasource=[[Datamanager datamodel] chaModel]; [self.table reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; [self.view addSubview:self.table]; } -(UITableView *)table{ if (!_table) { _table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; _table.delegate=self; _table.dataSource=self; } return _table; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _datasource.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]; } _table.rowHeight=100; MJEModel *mode=self.datasource[indexPath.row]; cell.textLabel.text=mode.title; cell.detailTextLabel.text=mode.category; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ youxiangqingViewController *xiangqing=[[youxiangqingViewController alloc]init]; MJEModel *model=self.datasource[indexPath.row]; xiangqing.StrUrl=model; [self.navigationController pushViewController:xiangqing animated:YES]; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ MJEModel *model=self.datasource[indexPath.row]; [[Datamanager datamodel]shanModel:model.ID]; self.datasource=[[Datamanager datamodel]chaModel]; [self.table reloadData]; } @end收藏界面点击单元格的详情界面 在youxiangqingViewController.h
#import "MJEModel.h" NS_ASSUME_NONNULL_BEGIN @interface youxiangqingViewController : UIViewController @property(nonatomic,strong)MJEModel *StrUrl; @end 在youxiangqingViewController.m里 #import "youxiangqingViewController.h" #import "Datamanager.h" @interface youxiangqingViewController () @end @implementation youxiangqingViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(right)]; // web 详情y界面 UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; NSURL *url=[NSURL URLWithString:self.StrUrl.url]; NSURLRequest *requst=[NSURLRequest requestWithURL:url]; [web loadRequest:requst]; [self.view addSubview:web]; } -(void)right{ [[Datamanager datamodel]zengModel:self.StrUrl]; } @end记住开网 就ok了
