2016-08-26 4 views
-5

URL APIからJSONをUITableViewに取得しようとしています。JSON to UITableView IOS Objective-C

以下は私のView Controllerのコードです。

私はObjective-Cの新機能です。

これを実現するために私が何を使うべきかを知りたいだけです。これは私のコードの下で、私はエラーが発生し続けます。

ご協力いただければ幸いです。

私は本当にこれが新しいので、詳しい説明と作業が必要です。

ViewController.h

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSError *error; 
    NSString *url_string = [NSString stringWithFormat: @"http://django-env2.55jfup33kw.us-west-2.elasticbeanstalk.com/api/Musician/?format=json"]; 
    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]]; 
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

    //NSLog(@"json: %@", json); 

    // NSString *mystring = json[1]; 
    // NSLog(@"json: %@", mystring); 


    NSMutableArray *myMutableArray = [json mutableCopy]; 

    NSArray *myArray = [myMutableArray copy]; 

    NSLog(@"json: %@", myArray); 


    self.greekLetters = @[@"test", @"test2"]; 




} 



- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return myArray.count; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSDictionary *cellDict = [myArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [cellDict objectForKey:@"address_line1"]; 
    cell.detailTextLabel.text = [cellDict objectForKey:@"zipcode"]; 

    return cell; 
} 

@end 
+0

コードの問題は何ですか? –

+1

エラーが発生しますか?どのようなエラー? 'cellDict'とは何ですか? JSONを取得したら '[myTableView reloadData];を実行する必要があります。 – Larme

+0

jsonがnilであるか、データソース配列がnilであるか、またはdelagteメソッドが呼び出されている場合は、すべての詳細を追加してください! –

答えて

1

ステップ-1

@interface ViewController() 
{ 
    // create the array 
    NSMutableArray *myMutableArray; 
} 
@end 

ステップ-2

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSError *error; 
    NSString *url_string = [NSString stringWithFormat: @"http://django-env2.55jfup33kw.us-west-2.elasticbeanstalk.com/api/Musician/?format=json"]; 
    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]]; 
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

     // allocate the memory of your array 
     myMutableArray = [NSMutableArray array]; 

    // check your dictionary contains values or not 
    if (json) 
    { 
    // add the data to your array 
    [myMutableArray addObject:json]; 
    [yourTableViewname reLoaddata]; // refresh the table 
    } 

} 

あなたのJSON出力は

です

enter image description here

ステップ-3

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return myMutableArray.count; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSDictionary *cellDict = [myMutableArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [cellDict objectForKey:@"first_name"]; 
    cell.detailTextLabel.text = [cellDict objectForKey:@"last_name"]; 

    return cell; 
}