標籤:
Model 類:
@interface ListModel : NSObject@property (nonatomic, copy)NSString *time;@property (nonatomic, copy)NSString *cname;@property (nonatomic, copy)NSString *summary;@property (nonatomic, copy)NSString *title;@property (nonatomic, copy)NSString *type;- (void)createArray:(NSDictionary *)result dataSource:(NSMutableArray *)dataSource;
VC:
#import "ViewController.h"#import "ListModel.h"#import "DetailViewController.h"#define URLSTR @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property (nonatomic, strong)NSMutableArray *dataSource;@property (nonatomic, strong)UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.title = @"新聞"; _dataSource = [[NSMutableArray alloc]initWithCapacity:0]; [self.view addSubview:self.tableView]; //建立一個非同步隊列解析 json,防止阻塞主線程 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^{ [self urlStr]; });}#pragma mark -- 解析 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 response, NSError * _Nullable error) { NSError *error1; //解析 json,返回字典,這裡解析出來是 unicode 編碼,不影響正常顯示 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error1]; ListModel *listModel = [[ListModel alloc]init]; [listModel createArray:dict dataSource:_dataSource]; //資料來源開始是空的,因為網路等原因...等資料來源有值了,在主線程重新整理 TabelView dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); }]; [task resume];}#pragma mark -- UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataSource.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *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; return cell;}#pragma mark -- 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;}#pragma mark -- getter- (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc]initWithFrame:self.view.frame]; _tableView.delegate = self; _tableView.dataSource = self; } return _tableView;}
完整代碼,在這裡下載
iOS學習 - 22 非同步解析 JSON,使用 Model 儲存,TableView 顯示