【问题标题】:JSON to UITableView IOS Objective-C [closed]JSON到UITableView IOS Objective-C [关闭]
【发布时间】:2016-08-26 13:26:56
【问题描述】:

我正在尝试从 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

【问题讨论】:

  • 代码有什么问题?
  • 遇到错误?什么错误?什么是打印cellDict?请注意,一旦获得 JSON,您应该[myTableView reloadData];
  • 如果您的 json 为 nil 或您的任何数据源数组为 nil 或调用了 delagte 方法,请添加所有详细信息!!!
  • Pavel 不要给出可能的重复来更好地回答这个问题

标签: ios objective-c json uitableview


【解决方案1】:

第一步

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

第二步

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多