iOS Learning-22 asynchronous parsing of JSON, using Model storage, TableView display

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.