JSON到UITableView IOS Objective-C

吉米

我正在尝试通过URL API将JSON转换为UITableView。

下面是我的视图控制器代码。

我是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
安布·卡尔提克(Anbu.Karthik)

步骤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输出是

在此处输入图片说明

第三步

  - (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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章