2013-10-21 52 views
0

我想解析一個包含圖像和一些標題和時間的一些鏈接的JSON文件。解析iOS中的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" 
    } 
] 

什麼我做錯了嗎?

回答

1

先試着調試這個。從崩潰日誌中,我認爲你對自定義單元格做錯了。首先檢查你的PictureJSON的連接。並檢查你的文件所有者它應該是你的自定義單元格。您可以使用斷點來確認這一點。如果您在創建單元時遇到錯誤,那麼以上就是解決方案。

+0

爲什麼要投票?至少需要一些努力來說明原因。 –

+0

好的兄弟你是對的,它是我的錯。 – SJS

+0

np .......它好。 –

0

我覺得這個問題是不是與你的JSON序列化,從你的日誌此崩潰,因爲這個標識符您static NSString *simpleTableIdentifier = @"PictureJSON";

確認,已分配的自定義單元格的!

+0

這不是問題,您可以使用任何字符串作爲標識符。 – danypata

+0

他正在使用[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; ,如果沒有任何具有此標識符的單元格,它將崩潰 – incmiko

2

我認爲你必須檢查屬性instaImage。我認爲您正在訪問您尚未在筆尖中定義的屬性。

+0

如果單元格不是單元格,則該單元格的其他兩個屬性也應引發異常。 – Bhavesh