Objective c 应用程序在使用JSON加载表视图中的数据时崩溃
我正在表视图中加载JSON数据。问题是如果我滚动表格视图,我的应用程序就会崩溃。代码如下:Objective c 应用程序在使用JSON加载表视图中的数据时崩溃,objective-c,uitableview,scroll,Objective C,Uitableview,Scroll,我正在表视图中加载JSON数据。问题是如果我滚动表格视图,我的应用程序就会崩溃。代码如下: - (void)viewDidLoad { [super viewDidLoad]; jsonurl=[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"]; jsonData=[[NSString alloc]initWithConten
- (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;
}
必须保留数组,因为要将它们分配给实例变量:
story = [[NSMutableArray array] retain];
description1 = [[NSMutableArray array] retain];
media1 = [[NSMutableArray array] retain];
如果在viewDidLoad之后需要这些实例变量,还必须保留它们:
必须保留数组,因为要将它们分配给实例变量:
story = [[NSMutableArray array] retain];
description1 = [[NSMutableArray array] retain];
media1 = [[NSMutableArray array] retain];
如果在viewDidLoad之后需要这些实例变量,还必须保留它们:
在.h文件中
简言之,在您的程序中,它们的数组没有Retain,因此您需要创建属性,或者它可以按照上面的答案编写简单的Retain。In.h文件
简言之,在您的程序中,它们对数组没有保留,因此您需要创建属性,或者它可能会像上面的回答那样编写简单的保留。您可以在此处发布崩溃日志吗?您可以在此处发布崩溃日志吗?请确保在dealloc方法中释放它们,并记住调用[super dealoc]。如果您使用的是ARC,则不需要保留或释放,因为IVAR会自动增强,ARC会自动为您保留和释放。请确保使用dealloc方法释放它们,并记住调用[super dealloc]。如果您使用的是ARC,则不需要保留或释放,因为IVAR自动强大,ARC将自动为您保留和释放。如果您不释放保留的属性,这将泄漏。您正在IVAR上调用release,因此您没有使用ARC。您已将luckyNumbers和arrForData声明为保留属性。您的dealloc方法不会释放或将您的属性分配给nil,因此您的代码会泄漏内存。ooopppsss sorrryy这需要从其他方面释放内存泄漏。如果出现内存泄漏,您是对的:-这将泄漏,因为您没有释放保留的属性。您正在IVAR上调用release,因此您没有使用ARC。您已将luckyNumbers和arrForData声明为保留属性。您的dealloc方法不会释放或将您的属性分配给nil,因此您的代码会泄漏内存。ooopppsss sorrryy这需要从其他方面释放,因为它是内存泄漏,您是对的:-
#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
#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