首先要先写一个json文件导入到Document文件里,
在浏览器上看OK的话,
这是一个外面是数组包含着的json文件
![](https://img.haomeiwen.com/i13357581/8e07f9bd7c06de4c.png)
先创建表格
定义个全局的数组
{
NSArray *_tableData;
}
在viewDidLoad里:
NSString *urlStr = @"";//网址
NSURL *url = [NSURL URLWithString:urlStr];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"服务器连接失败");
return ;
}
NSError *jsonError = nil;
_tableData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
if (jsonError) {
NSLog(@"解析错误");
return;
}
//回到主线程刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
[self.table reloadData];
});
}] resume];
再在表格内容里写:
NSDictionary *cellDic = _tableData[indexPath.row];
cell.textLabel.text = cellDic[@"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@,%@",cellDic[@"money"],cellDic[@"address"]];
网友评论