首先要先寫一個(gè)json文件導(dǎo)入到Document文件里杯活,
在瀏覽器上看OK的話棒动,
這是一個(gè)外面是數(shù)組包含著的json文件
先創(chuàng)建表格
定義個(gè)全局的數(shù)組
{
NSArray *_tableData;
}
在viewDidLoad里:
NSString *urlStr = @"";//網(wǎng)址
NSURL *url = [NSURL URLWithString:urlStr];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"服務(wù)器連接失敗");
return ;
}
NSError *jsonError = nil;
_tableData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
if (jsonError) {
NSLog(@"解析錯(cuò)誤");
return;
}
//回到主線程刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
[self.table reloadData];
});
}] resume];
再在表格內(nèi)容里寫:
NSDictionary *cellDic = _tableData[indexPath.row];
cell.textLabel.text = cellDic[@"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@,%@",cellDic[@"money"],cellDic[@"address"]];