【问题标题】:How to fill UITableView with ASIHttpRequest JSON data?如何用 ASIHttpRequest JSON 数据填充 UITableView?
【发布时间】:2012-09-25 20:50:15
【问题描述】:

我正在尝试使用 ASIhttprequest 用 this JSON data 填充我的 UITableView,但我得到以下异常:__NSArrayM objectForKey:]: unrecognized selector sent to instance

我在我的控制器中使用以下代码来实现这一点:

#import <UIKit/UIKit.h>

@interface tableListViewController : UITableViewController {
    NSDictionary *data;
}

@end

还有主文件:

#import "tableListViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@interface tableListViewController ()

@end

@implementation tableListViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://api.glennzo.nl/glow/index.json"];
    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTag:100];
    [request setDelegate:self];
    [request startSynchronous];

    //Set the title
    self.navigationItem.title = @"Events";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void) requestFinished:(ASIHTTPRequest *) request
{
    if (request.tag == 100) {
        NSData *responseData = [request responseData];
        NSError *error;

        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

        if(!JSON || ![JSON count])
        {
            NSLog(@"%@", @"geen correcte json data van request gekregen");
        }
        else
        {
            data = JSON;
        }
    }
}

- (void) requestFailed:(ASIHTTPRequest *) request
{
    NSError *error = [request error];
    NSLog(@"Error: %@",error.localizedDescription);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [data count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [UITableViewCell alloc];
    }

    NSArray *namen = [data objectForKey:@"name"];
    cell.textLabel.text = [namen objectAtIndex:indexPath.row];

    return cell;
}

多亏了一些调试,我现在知道 ASIHttpRequest 可以工作,但是我创建的 NSdictionary(并打算在整个应用程序中使用)给我带来了麻烦。有人可以让我朝着正确的方向前进吗?

【问题讨论】:

    标签: objective-c ios json uitableview asihttprequest


    【解决方案1】:

    错误似乎在:

    NSArray *namen = [data objectForKey:@"name"];
    

    所以我认为data 类型不正确(NSDictionary),为 nil 或填充了另一种类型:

    [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
    

    尝试查看通过 JSON 传递给您的内容。例如:

    NSError *jsonError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
    
    if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"its an array!");
        NSArray *jsonArray = (NSArray *)jsonObject;
        NSLog(@"jsonArray - %@",jsonArray);
        } else {
        NSLog(@"its probably a dictionary");
        NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
        NSLog(@"jsonDictionary - %@",jsonDictionary);
    }
    

    【讨论】:

    • 感谢您的回答,我应该提到我对 IOS 开发很陌生。制作数据和 JSON 类型的 ID 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多