iOS - 數(shù)據(jù)請求解析

在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:@"網(wǎng)絡鏈接" 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 = @"內(nèi)容";
    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;

然后在數(shù)據(jù)庫.h里設置增刪改查
@interface datamanager : NSObject
+(datamanager *)dataModel;
-(void)zengModel:(id)zeng;
-(void)shanModel:(int)shan;
-(id)chaModel;
@end
然后在數(shù)據(jù)庫.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

在收藏里設置內(nèi)容
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里設置數(shù)據(jù)庫負責傳值
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];
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子守伸,更是在濱河造成了極大的恐慌谆沃,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異痢毒,居然都是意外死亡冠王,警方通過查閱死者的電腦和手機米丘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門剑令,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拄查,你說我怎么就攤上這事吁津。” “怎么了堕扶?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵碍脏,是天一觀的道長。 經(jīng)常有香客問我稍算,道長典尾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任糊探,我火速辦了婚禮钾埂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘科平。我一直安慰自己褥紫,他們只是感情好,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布瞪慧。 她就那樣靜靜地躺著髓考,像睡著了一般。 火紅的嫁衣襯著肌膚如雪弃酌。 梳的紋絲不亂的頭發(fā)上氨菇,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音妓湘,去河邊找鬼查蓉。 笑死,一個胖子當著我的面吹牛多柑,可吹牛的內(nèi)容都是我干的奶是。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼竣灌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了秆麸?” 一聲冷哼從身側(cè)響起初嘹,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沮趣,沒想到半個月后屯烦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年驻龟,在試婚紗的時候發(fā)現(xiàn)自己被綠了温眉。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡翁狐,死狀恐怖类溢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情露懒,我是刑警寧澤闯冷,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站懈词,受9級特大地震影響蛇耀,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜坎弯,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一纺涤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抠忘,春花似錦洒琢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至荧嵌,卻和暖如春呛踊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背啦撮。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工谭网, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赃春。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓愉择,卻偏偏與公主長得像,于是被迫代替她去往敵國和親织中。 傳聞我的和親對象是個殘疾皇子锥涕,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 1 CALayer IOS SDK詳解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi閱讀 5,148評論 3 23
  • MVVM模式與團隊合作 說到架構(gòu)設計和團隊協(xié)作,這個對App的開發(fā)還是比較重要的狭吼。即使作為一個專業(yè)的搬磚者层坠,前提是...
    GitHubPorter閱讀 6,663評論 9 19
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,097評論 1 32
  • 今天我們來做個解析加收藏的功能 我們用在pods中導入target '項目名稱' dopod 'AFNetwork...
    蓋世英雄的夢想閱讀 325評論 0 0
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,709評論 0 9