使用JSON在表视图中加载数据时,应用程序崩溃

时间:2011-08-02 12:54:22

标签: objective-c uitableview scroll

我正在表视图中加载JSON数据。问题是,如果我滚动表视图,我的应用程序崩溃。以下是代码:

    - (void)viewDidLoad {
    [super viewDidLoad];
    jsonurl=[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"];

    jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];

    jsonArray = [jsonData JSONValue]; 


    items = [jsonArray objectForKey:@"items"];

    story = [NSMutableArray array];
    description1 = [NSMutableArray array];
    media1 = [NSMutableArray array];

    for (NSDictionary *item in items )
    {
        [story addObject:[item objectForKey:@"title"]];
        [media1 addObject:[item objectForKey:@"media"]];


    }
     NSLog(@"booom:%@",story);

}

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [story 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:UITableViewStyleGrouped reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text=[story objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[media1 objectAtIndex:indexPath.row];
    return cell;
}

2 个答案:

答案 0 :(得分:5)

您必须保留数组,因为您要将它们分配给实例变量:

story = [[NSMutableArray array] retain];
description1 = [[NSMutableArray array] retain];
media1 = [[NSMutableArray array] retain];

如果在viewDidLoad之后需要它们,你还必须保留这些实例变量:

jsonurl
jsonArray
items

答案 1 :(得分:2)

在.h文件中

#import <UIKit/UIKit.h>

@interface LuckyNumbersViewController : UIViewController {
    IBOutlet UILabel *label;
    NSMutableData *responseData;
    NSArray *luckyNumbers;
    IBOutlet UITableView *tbl;
    NSMutableArray *arrForData;
}
@property(nonatomic,retain) NSArray *luckyNumbers;
@property(nonatomic,retain) NSMutableArray *arrForData;
@end

在.m文件中

#import "LuckyNumbersViewController.h"
#import "JSON/JSON.h"

@implementation LuckyNumbersViewController
@synthesize luckyNumbers;
- (void)viewDidLoad {   
    [super viewDidLoad];

    responseData = [[NSMutableData data] retain];       
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];            
}

- (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 {
    label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release]; 
    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    self.luckyNumbers = [json objectWithString:responseString error:&error];
    NSLog(@"numbers of the items are ::>>%i",[[self.luckyNumbers valueForKey:@"items"]count]);
    [responseString release];
    [tbl reloadData];
}
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.luckyNumbers valueForKey:@"items"]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];
    }

    cell.textLabel.text=[[[self.luckyNumbers valueForKey:@"items"]objectAtIndex:indexPath.row]valueForKey:@"title"];
    // Set up the cell...

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
    // [self.navigationController pushViewController:anotherViewController];
    // [anotherViewController release];
}


- (void)dealloc {
    [super dealloc];
}

@end

简而言之,在你的程序中,他们没有保留数组,因此你需要创建属性,或者它可以写上简单的保留,如上面的答案。