将JSON图像URL解析为自定义表视图错误

时间:2013-10-15 16:51:24

标签: ios json xcode parsing

我正在尝试将包含一些url的JSON文件解析为图片,标题和文本。我试图以相同的方式解析另一个JSON文件,但只有文本才有效。但是这个不行。这是我的代码:

·H:

#import <UIKit/UIKit.h>

@interface PicturesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
    NSURLConnection *conn;
    NSMutableData *responseData;
    NSMutableArray *news;
    NSIndexPath * indexPath;
    UIActivityIndicatorView *loading;
}

@property (nonatomic, retain) NSIndexPath * indexPath;
@property (nonatomic, strong) NSArray *tweets;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;

@end

的.m:

#import "PicturesViewController.h"
#import "AppDelegate.h"
#import "RNBlurModalView.h"
#import "PictureJSON.h"

@interface PicturesViewController ()
{
    NSInteger refreshIndex;
    NSArray *images;
}

@end

@implementation PicturesViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(showMenu)];

    UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    [self.view addGestureRecognizer:gestureRecognizer];

     [self issueLoadRequest];
}

- (void)swipeHandler:(UIPanGestureRecognizer *)sender
{
    [[self sideMenu] showFromPanGesture:sender];
}

#pragma mark -
#pragma mark Button actions

- (void)showMenu
{
    [[self sideMenu] show];
}

#pragma mark - Table view data source


- (void)issueLoadRequest
{
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://my-site/pictureparse.php?name=MyName"]];
        [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
    });
}

- (void)receiveData:(NSData *)data {
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
    self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [self.myTableView reloadData];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PictureJSON";

    PictureJSON *cell = (PictureJSON *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PictureJSON" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
    cell.instaImage = [tweet objectForKey:@"link"];
    cell.titleLabel.text = [tweet objectForKey:@"title"];
    cell.timeLabel.text = [tweet objectForKey:@"published"];

    return cell;
}

@end

我有一个名为PictureJSON的自定义表视图文件,其中包含一个如下所示的nib文件:

enter image description here

但是当我启动我的应用程序时,我收到此错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PicturesViewController 0x17d86dc0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key instaImage.'

有人可以帮我解决这个问题吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

可能你的JSON没有很好的结构,请在JSONLint - The JSON Validator中查看查询结果,也许这可以帮助你检测错误。