【问题标题】:Parsing JSON image urls to custom Table View error将 JSON 图像 URL 解析为自定义表视图错误
【发布时间】:2013-10-15 16:51:24
【问题描述】:

我正在尝试将包含一些 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 文件如下所示:

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

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

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

【问题讨论】:

  • 就在cell.instaImage = [tweet objectForKey:@"link"];这一行之前(这是导致异常的原因),您是否检查过cellPictureJSON 而不是UITableViewCell 的实例?
  • 嗯,我不确定。你怎么检查? @尼尔科
  • 一个简单的方法是在我提到的行之前添加它:NSLog(@"%@", [cell class]); 这会将单元格类型的名称写入 Xcode 中的控制台。

标签: ios json xcode parsing


【解决方案1】:

可能你得到的 JSON 结构不好,检查JSONLint - The JSON Validator 中的查询结果也许这可以帮助你发现错误。

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    相关资源
    最近更新 更多