2016-11-06 9 views
-1

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 
+0

次回は他の質問をさらに慎重に検討していただきますように、ありがとうございます!ハッピーコーディング! –

+0

@SyedeHussaini他の人を指し示すつもりなら、答えの中の問題を修正する必要があります。 – rmaddy

+0

また、あなたのリンクはSwiftのものでした。私の現在のアプリケーションはObj-cで書かれているので、関連性はありません。 :P –

答えて

0

.. 。

その時点であなたの配列は何の要素を持っていないし、あなたのAPI呼び出しの成功ブロックが実行されたとき、あなたはRを呼び出す必要がありながら、あなたはのviewDidLoadメソッドであなたのtableViewをリロードしている
dispatch_async(dispatch_get_main_queue(), ^{ 
       NSLog(@"works."); 

      self.dataList = (NSArray *)[NSJSONSerialization JSONObjectWithData:data 
                 options:0 
                  error:nil]; 

      [self.tableView reloadData]; 
      // this NSlog prints the JSON data successfully.  
      NSLog(@"oo%@ and array %@",_dataList,_array); 




      }); 

eloadDataメソッド。

+0

ああ..私はすでにviewDidLoadでそのメソッドを持っていますか?これが違いをもたらすと思いますか? - (void)viewDidLoad { [super viewDidLoad]; [self readDataFromFile]; [self.tableView reloadData]; } –

+0

これはまだ実行されません。 APIの成功ブロックがいつ呼び出されるかわからないからです。したがって、配列にデータがロードされているときにデータをリロードする必要があるため、データを配列にロードする行の下にreloadDataメソッドを呼び出します。 –

+0

私のケースをよく検討していただきありがとうございます。それはそれを解決しました! Happy iOSコーディング! :) –