首页 > 代码库 > iOS学习 - 22 异步解析 JSON,使用 Model 存储,TableView 显示
iOS学习 - 22 异步解析 JSON,使用 Model 存储,TableView 显示
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 显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。