开发者

App is crashing while loading data in table view using JSON

I'm loading JSON data in a table view. The problem is that if I'm scrolling the table view, my app crashes. Below is the code:

    - (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)sec开发者_开发百科tion {
    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;
}


You have to retain the arrays, since you're assigning them to instance variables:

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

You'll also have to retain these instance variables, if you need them after viewDidLoad:

jsonurl
jsonArray
items


In .h file

#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

In .m File

#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

In short in Your Program their is no Retain for array thus you need to create property or it may write simple retain as above answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜