AFN??加MJ解析

@interface Model : NSObject

@property(nonatomic , strong)NSString *name;

@property(nonatomic , strong)NSString *start_time;

@property(nonatomic , strong)NSString *create_time;

@property(nonatomic , strong)NSString *describe;

@property(nonatomic , strong)NSString *type;

@end

表格

- (void)setvaluesModel:(Model*)model{

? ? if(model) {

? ? ? ? self.label1.text= model.name;

? ? ? ? self.label2.text= model.describe;

? ? ? ? self.label3.text= model.start_time;

? ? }

}


#import "ViewController.h"

#import "AFHTTPSessionManager.h"

#import "TableViewCell.h"

#import "Model.h"

#import "DataManager.h"

#import "Student+CoreDataClass.h"

#import "MJExtension.h"


- (void)Load{

? ? AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

? ? [managerGET:@"http://127.0.0.1/first.json"parameters:nilprogress:^(NSProgress*_NonnulldownloadProgress) {


? ? }success:^(NSURLSessionDataTask*_Nonnulltask,id? _NullableresponseObject) {

? ? ? ? self.array = [Model mj_objectArrayWithKeyValuesArray:responseObject];

? ? ? ? for(NSDictionary*dicinresponseObject) {

? ? ? ? ? ? [[DataManagershareManager]insert:dic];

? ? ? ? }

? ? ? ? [self.ojtablereloadData];

? ? }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

? ? ? ? NSLog(@"%@",error);

? ? }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

? ? returnarr.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

? ? if(section ==0) {

? ? ? ? return1;

? ? }else{

? ? ? ? returnself.array.count;

? ? }

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

? ? if(self.array.count>0) {

? ? ? ? Model*model =self.array[indexPath.row];

? ? ? ? [cellsetvaluesModel:model];

? ? ? ? [cell.Btn addTarget:self action:@selector(StartBtn:)forControlEvents:UIControlEventTouchUpInside];

? ? ? ? [cell.Btn setTitle:@"開始考試" forState:UIControlStateNormal];

? ? ? ? cell.Btn.tag= indexPath.row;

? ? }

? ? returncell;

}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? return 150;

}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

? ? returnarr[section];

}

- (void)StartBtn:(UIButton*)btn{

}

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

? ? Student*model =self.array[indexPath.row];

? ? [[DataManager shareManager]deleteData:model];

? ? [self.arrayremoveObject:self.array[indexPath.row]];

? ? [self.ojtable reloadData];

}


下面這個是POST解析


@interfaceImgModel :NSObject

@property(nonatomic,assign)NSDate *create_time;

@property(nonatomic,copy)NSString *id;

@property(nonatomic,copy)NSString *title;

@end

@interfaceDataModel :NSObject

@property(nonatomic,assign) int page;

@property(nonatomic,assign) int pages;

@property(nonatomic,copy) NSArray *list;

@end

@interface Model : NSObject

@property(nonatomic,assign) int code;

@property(nonatomic,strong) DataModel *data;

@end

#import "Model.h"

@implementation ImgModel

@end

@implementation DataModel

@end

@implementation Model

@end

#import "ViewController.h"

#import "Model.h"

#import "AFNetworking.h"

#import "MJExtension.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *arr;

@property(nonatomic,strong)UITableView *tbv;

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];



? ? [self createNav];


? ? [self.viewaddSubview:self.tbv];


}

-(void)createNav{



? ? //2.發(fā)送GET請求

? ? /*

?? ? 第一個參數(shù):請求路徑(不包含參數(shù)).NSString

?? ? 第二個參數(shù):字典(發(fā)送給服務(wù)器的數(shù)據(jù)~參數(shù))

?? ? 第三個參數(shù):progress 進(jìn)度回調(diào)

?? ? 第四個參數(shù):success 成功回調(diào)

?? ? task:請求任務(wù)

?? ? responseObject:響應(yīng)體信息(JSON--->OC對象)

?? ? 第五個參數(shù):failure 失敗回調(diào)

?? ? error:錯誤信息

?? ? 響應(yīng)頭:task.response

?? ? */


? ? AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];


? ? //參數(shù)

? ? NSMutableDictionary *params = [NSMutableDictionary dictionary];

? ? params[@"method"] = @"app.news.getarticlelist";

? ? params[@"class_id"] =@"3";

? ? params[@"_debug"] =@"Y";


? ? [managerPOST:@"http://test.bit.api.meeboo.cc"parameters:paramsprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id? _NullableresponseObject) {


? ? ? ? [DataModel mj_setupObjectClassInArray:^NSDictionary *{

? ? ? ? ? ? return@{

?? ? ? ? ? ? ? ? ? ? @"list":@"ImgModel"

?? ? ? ? ? ? ? ? ? ? };

? ? ? ? }];


? ? ? ? Model*model = [Modelmj_objectWithKeyValues:responseObject];


? ? ? ? self.arr= model.data.list;


? ? ? ? [self.tbvreloadData];



? ? ? ? NSLog(@"%@---%@",[responseObjectclass],responseObject);




? ? }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

? ? ? ? NSLog(@"請求失敗--%@",error);

? ? }];



}

-(UITableView *)tbv{

? ? if(!_tbv) {

? ? ? ? _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

? ? }


? ? _tbv.dataSource = self;

? ? _tbv.delegate=self;


? ? return _tbv;


}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

? ? return_arr.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


? ? if(!cell) {

? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

? ? }


? ? //cell.textLabel.text = [self.arr[indexPath.row] objectForKey:@"title"];

? ? ImgModel*mod=self.arr[indexPath.row];

? ? cell.textLabel.text= mod.title;


? ? returncell;


}



AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

? ? [manager GET:@"http://123.126.40.109:7003/asmr/videos/A1100101.shtml" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {


? ? } success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {

? ? ? ? //解析的是數(shù)組

? ? ? ? NSArray *dicArr = responseObject[@"result"];

? ? ? ? self.array = [NSArray yy_modelArrayWithClass:[Model class] json:dicArr];

? ? ? ? //解析是字典

//? ? ? ? Model *model = [Model yy_modelWithJSON:responseObject];

//? ? ? ? for (NSDictionary *dic in responseObject[@"result"]) {

//? ? ? ? ? ? Model *model = [Model new];

//? ? ? ? ? ? [model setValuesForKeysWithDictionary:dic];

//? ? ? ? ? ? [self.array addObject:model];

//? ? ? ? }

? ? ? ? [self.ojtable reloadData];

? ? } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

? ? ? ? NSLog(@"%@",error);

? ? }];

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惨寿,一起剝皮案震驚了整個濱河市壳咕,隨后出現(xiàn)的幾起案子匀哄,更是在濱河造成了極大的恐慌奉瘤,老刑警劉巖罩息,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異洲押,居然都是意外死亡煮盼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門远舅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闰蛔,“玉大人,你說我怎么就攤上這事图柏⌒蛄” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵蚤吹,是天一觀的道長例诀。 經(jīng)常有香客問我随抠,道長,這世上最難降的妖魔是什么繁涂? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任拱她,我火速辦了婚禮,結(jié)果婚禮上扔罪,老公的妹妹穿的比我還像新娘秉沼。我一直安慰自己,他們只是感情好矿酵,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布唬复。 她就那樣靜靜地躺著垛叨,像睡著了一般巩搏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上偶翅,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天辜腺,我揣著相機(jī)與錄音休建,去河邊找鬼。 笑死哪自,一個胖子當(dāng)著我的面吹牛丰包,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播壤巷,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼瞧毙!你這毒婦竟也來了胧华?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宙彪,失蹤者是張志新(化名)和其女友劉穎矩动,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體释漆,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡悲没,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了男图。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片示姿。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖逊笆,靈堂內(nèi)的尸體忽然破棺而出栈戳,到底是詐尸還是另有隱情,我是刑警寧澤难裆,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布子檀,位于F島的核電站镊掖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏褂痰。R本人自食惡果不足惜亩进,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缩歪。 院中可真熱鬧归薛,春花似錦、人聲如沸驶冒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骗污。三九已至崇猫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間需忿,已是汗流浹背诅炉。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留屋厘,地道東北人涕烧。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像汗洒,于是被迫代替她去往敵國和親议纯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

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

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件溢谤,我們平時使用的軟件中到處都可以看到它的影子瞻凤,類似...
    liudhkk閱讀 9,052評論 3 38
  • 一、簡介 <<UITableView(或簡單地說世杀,表視圖)的一個實例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,609評論 3 3
  • 前言 最近忙完項目比較閑阀参,想寫一篇博客來分享一些自學(xué)iOS的心得體會,希望對迷茫的你有所幫助瞻坝。博主非科班出身蛛壳,一些...
    GitHubPorter閱讀 1,430評論 9 5
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,103評論 1 32
  • 六盤天高白云悠, 風(fēng)塵一日至原州所刀。 欲窮夜暮景色美衙荐, 攜友同去登古樓。
    魏加恩閱讀 299評論 0 1