Model class:
@interface* * * * * type; -(void) Createarray: (nsdictionary *) result *) DataSource;
Vc:
#import "ViewController.h"#import "ListModel.h"#import "DetailViewController.h"#defineURLSTR @ "http://ipad-bjwb.bjd.com.cn/digitalpublication/publish/handler/apinewslist.ashx?date=20131129&startrecord=1 &len=5&udid=1234567890&terminaltype=iphone&cid=213 "@interfaceViewcontroller () <UITableViewDelegate,UITableViewDataSource>@property (nonatomic, strong) Nsmutablearray*DataSource, @property (nonatomic, strong) UITableView*TableView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.title=@"News"; _datasource= [[Nsmutablearray alloc]initwithcapacity:0]; [Self.view AddSubview:self.tableView]; //Create an asynchronous queue parsing JSON to prevent the main thread from blockingdispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_high,0); Dispatch_async (Queue,^{[Self urlstr]; });}#pragmaMark--Analytic json-(void) urlstr{Nsurl*url =[Nsurl Urlwithstring:urlstr]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurlsessiondatatask*task = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable RESPO NSE, Nserror *_nullable Error) {Nserror*Error1; //parse JSON, return dictionary, here parse out is Unicode encoding, do not affect normal displayNsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:&Error1]; Listmodel*listmodel =[[Listmodel alloc]init]; [Listmodel createarray:dict Datasource:_datasource]; //The data source started to be empty because of network and other reasons ... When the data source has value, the main thread refreshes the TabelviewDispatch_async (Dispatch_get_main_queue (), ^{[_tableview reloaddata]; }); }]; [Task resume];}#pragmaMark--uitableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return_datasource.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *cell_id =@"Cell"; UITableViewCell*cell =[TableView dequeuereusablecellwithidentifier:cell_id]; if(!cell) {Cell=[[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cell_id]; } Listmodel*listmodel =_datasource[indexpath.row]; Cell.textLabel.text=Listmodel.title; Cell.detailTextLabel.text=Listmodel.time; returncell;}#pragmaMark--uitableviewdelegate-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {Listmodel*listmodel =_datasource[indexpath.row]; Detailviewcontroller*DETAILVC =[[Detailviewcontroller alloc]init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:DETAILVC Animated:yes]; Detailvc.titlestr=Listmodel.cname; Detailvc.contentstr=listmodel.summary;}#pragmaMark--getter-(UITableView *) TableView {if(!_tableview) {_tableview=[[UITableView alloc]initwithframe:self.view.frame]; _tableview.Delegate=Self ; _tableview.datasource=Self ; } return_tableview;}
Complete code, download here
iOS Learning-22 asynchronous parsing of JSON, using Model storage, TableView display