Я пробую парсер JSON впервые и мне нужна небольшая помощь
Когда я пытаюсь заполнить табличное представление, оно работает нормально, но когда я прокручиваю таблицу или выбираю строку, приложение вылетает. Буду признателен за любую помощь.
Вот файлы, которые у меня есть:
#import <UIKit/UIKit.h>
@class RootViewController;
@interface BooksJsonAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *statuses;
NSMutableData *responseData;
}
@property(nonatomic, retain)NSMutableArray *statuses;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
и
#import "BooksJsonAppDelegate.h"
#import "RootViewController.h"
#import "SBJson.h"
@implementation BooksJsonAppDelegate
@synthesize window;
@synthesize navigationController,statuses;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://assignment.golgek.mobi/api/v10/items"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
statuses = [parser objectWithString:json_string error:nil];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection Failed: %@",[error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
@end
then the root view controller
#import <UIKit/UIKit.h>
@class DetailView,BooksJsonAppDelegate;
@interface RootViewController : UITableViewController {
DetailView *detailView;
BooksJsonAppDelegate *booksAppDelegate;
}
@end
и
#import "RootViewController.h"
#import "DetailView.h"
#import "BooksJsonAppDelegate.h"
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
booksAppDelegate = (BooksJsonAppDelegate *)[[UIApplication sharedApplication] delegate];
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [booksAppDelegate.statuses count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];
cell.textLabel.text = [aBook objectForKey:@"title"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];
detailView = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailView.title = [aBook objectForKey:@"title"];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end