0

我正在尝试解析一个 JSON 文件,其中包含一些图像链接以及一些标题和时间。

这是我的代码:

#import "PicturesViewController.h"
#import "DemoViewController.h"
#import "SecondViewController.h"
#import "AppDelegate.h"
#import "RNBlurModalView.h"
#import "PictureJSON.h"
#import "HMSegmentedControl.h"

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

@end

@implementation PicturesViewController

- (void)viewDidLoad
{

    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Instagram", @"Hashtags", @"Facebook"]];
    [segmentedControl setFrame:CGRectMake(10, 10, 300, 60)];
    [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];

    [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];
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl1 {
    [self issueLoadRequest];
}

- (void)segmentedControlSelectedIndexChanged:(id)sender
{
    [self issueLoadRequest];
}

#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=Name"]];
        [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
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
    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];
    }

    // The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
    NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
    NSLog(@"%@", [cell class]);
    cell.instaImage = [tweet objectForKey:@"link"];
    cell.titleLabel.text = [tweet objectForKey:@"title"];
    cell.timeLabel.text = [tweet objectForKey:@"published"];

    return cell;
}

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

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

我的 JSON 文件如下所示:

[
    {
        "link": "http://link.com/picture.jpg",
        "title": "title",
        "published": "0:12 PM 21/10"
    },
    {
        "link": "http://link.com/picture.jpg",
        "title": "title",
        "published": "0:09 AM 21/10"
    }
]

我究竟做错了什么?

4

3 回答 3

2

我认为您必须检查属性 instaImage。我认为您正在访问您尚未在 nib 中定义的属性。

于 2013-10-21T12:02:47.913 回答
1

拳头尝试调试这个。从崩溃日志中,我认为您对自定义单元做错了。首先检查 PictureJSON 的连接。并检查您的文件所有者,它应该是您的自定义单元格。您可以使用 breakpoint 确认这一点。如果您在创建单元格时遇到错误,那么上面就是解决方案。

于 2013-10-21T12:01:10.090 回答
0

我认为问题不在于你的 JSON 序列化,从你的日志中看这个崩溃是因为你的 static NSString *simpleTableIdentifier = @"PictureJSON";

确保您已为您的自定义单元格分配了此标识符!

于 2013-10-21T12:02:33.330 回答