美文网首页
iOS - 数据请求解析

iOS - 数据请求解析

作者: 繁重 | 来源:发表于2019-01-20 10:11 被阅读0次

    在CocoaPods导入
    pod 'AFNetworking'
    pod 'MBProgressHUD'
    导入头文件 并建议下头文件
    先建头文件
    MJExtension.h
    AFNetworking.h
    MBProgressHUD.h
    Model.h
    shoucangViewController.h
    xiangqingViewController.h
    tiaoViewController.h

    <UITableViewDelegate,UITableViewDataSource>
    @property(nonatomic,strong)UITableView *tab;
    @property(nonatomic,strong)NSMutableArray *array;
    
    - (void)viewDidLoad {
        [super 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:@"网络链接" 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;
        cell.textLabel.numberOfLines = 0;
        cell.detailTextLabel.numberOfLines = 0;
        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];
    }
    

    然后在tiao控制器.h导入
    Model.h
    @property(nonatomic,strong)Model *mo;
    然后.m敲一下代码
    datamanager.h
    @property(nonatomic,strong)UIWebView *web;

    - (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];
    }
    

    在Model.h里设置变量

    @property(nonatomic,strong)NSString *title,*url,*category;
    @property(nonatomic,assign)int *ID;
    

    然后在数据库.h里设置增删改查
    @interface datamanager : NSObject
    +(datamanager *)dataModel;
    -(void)zengModel:(id)zeng;
    -(void)shanModel:(int)shan;
    -(id)chaModel;
    @end
    然后在数据库.m里

    #import "datamanager.h"
    #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 ];
    }
    @end
    

    在收藏里设置内容
    Model.h
    xiangqingViewController.h
    datamanager.h
    <UITableViewDelegate,UITableViewDataSource>
    @property(nonatomic,strong)UITableView *tab;
    @property(nonatomic,strong)NSMutableArray *array;

    -(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;
        cell.textLabel.numberOfLines = 0;
        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];
    }
    

    详情里设置model负责传值
    Model.h
    @property(nonatomic,strong)Model *mod;
    在.m里设置数据库负责传值
    datamanager.h
    @property(nonatomic,strong)UIWebView *web;

    - (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];
    }
    

    相关文章

      网友评论

          本文标题:iOS - 数据请求解析

          本文链接:https://www.haomeiwen.com/subject/vfztjqtx.html