JSON和iPhone表格视图

我第一次尝试JSONparsing器,我需要一点帮助当我尝试填充表视图它工作正常,但是当我滚动表或select行应用程序崩溃。 我将不胜感激任何帮助。

这里是我有的文件:

#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 

您可能需要保留您的状态

 statuses = [[parser objectWithString:json_string error:nil] retain]; 

JSONparsing器将返回一个自动释放的对象:)


正如Dan在评论中指出的那样,更好的做法是设置这样的属性:

 self.statuses = [parser objectWithString:json_string error:nil]; 

这样做的好处是不会泄漏内存,如果你设置两次,你可以使用KVO不知道它是否改变。 好多了 :)

你确定booksAppDelegate.statuses正确填充?

你可以通过loggingjson进程来检查这个statuses = [parser objectWithString:json_string error:nil]; ,在statuses = [parser objectWithString:json_string error:nil]; 只需添加以下行: NSLog(@"%@",[statuses description]); 看什么数据被放入字典