-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
コードの問題は何ですか? –
エラーが発生しますか?どのようなエラー? 'cellDict'とは何ですか? JSONを取得したら '[myTableView reloadData];を実行する必要があります。 – Larme
jsonがnilであるか、データソース配列がnilであるか、またはdelagteメソッドが呼び出されている場合は、すべての詳細を追加してください! –