WebサーバーからJSONデータをtableviewcontrollerに追加しようとしていました。これは私の現在のコードです。JSON to Tableview
_datalistのNSlogはJSONを出力しますが、実際にデータをセルに入れるための他のメソッドはありません。コードは正しいように見えますが、_datalist NSArrayは実際にデータを取得するため、これを処理してください。私はあなたの助けと助言が必要なのです。私は考え出したものについては
、細胞はJSONデータで初期化されている方法は、どちらかそれをフェッチする、またはそれを使用することはできません。私の手掛かりは、着信データをエンコードする必要があるということでしたか?
これらの方法のいずれもが実行されていない#import "TopUsersViewController.h"
@interface TopUsersViewController()
@property NSArray* dataList;
@end
@implementation TopUsersViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self readDataFromFile];
[self.tableView reloadData];
}
-(void)readDataFromFile
{
NSString *urlAsString = @"http://myURL.com/json.json";
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
NSLog(@"%@", urlAsString);
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (error)
{
// no internet/can't fetch!
NSLog(@"Failure %@",error.localizedDescription);
}
NSLog(@"Success!");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"works.");
self.dataList = (NSArray *)[NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
// this NSlog prints the JSON data successfully.
NSLog(@"oo%@ and array %@",_dataList,_array);
});
}];
}
// ..あなたは、あなたがあなたの配列のデータを持っているとき、あなたのtableViewをリロードしたいデリゲートメソッドで、あなたのtableView
を伝える必要があり
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// count the initialized array from viewDidLoad
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"return the data");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
id keyValuePair = self.dataList[indexPath.row];
cell.textLabel.text = keyValuePair[@"identity"];
cell.detailTextLabel.text=[NSString stringWithFormat:@"Points: %@", keyValuePair[@"points"]];
return cell;
}
@end
次回は他の質問をさらに慎重に検討していただきますように、ありがとうございます!ハッピーコーディング! –
@SyedeHussaini他の人を指し示すつもりなら、答えの中の問題を修正する必要があります。 – rmaddy
また、あなたのリンクはSwiftのものでした。私の現在のアプリケーションはObj-cで書かれているので、関連性はありません。 :P –