JSON和iPhone表视图 [英] JSON and iPhone table view

查看:73
本文介绍了JSON和iPhone表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用JSON解析器,我需要一些帮助当我尝试填充表格视图时,它可以正常工作,但是当我滚动表格或选择一行时,应用程序崩溃.我将不胜感激.

I'm trying JSON parser for first time and i need a little help When I try to populate a table view it works OK, but when I scroll the table or select a row the app crashes. I would appreciate any help.

这是我的文件:

#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

推荐答案

您可能需要保留自己的状态

You will probably need to retain your statuses

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

JSON解析器将返回一个自动释放的对象:)

The JSON parser will return an autoreleased object :)

如丹在评论中所指出的那样,更好的方法是将属性设置如下:

As Dan points out in the comments the better way of doing this is to set the property like this :

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

如果设置两次,它的优点是不会泄漏内存,并且可以使用KVO ot告诉它是否已更改.好多了:)

This has the advantage of not leaking memory if you set it twice and you can use KVO ot tell if it's changed. Much better :)

这篇关于JSON和iPhone表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆