【问题标题】:JSON and Iphone table loading, I'm getting a EXC_BAD_ACCESS error when scrollingJSON 和 Iphone 表加载,滚动时出现 EXC_BAD_ACCESS 错误
【发布时间】:2010-06-01 18:32:59
【问题描述】:

我很想弄清楚为什么会出现 EXC_BAD ACCESS 错误。控制台给了我这个错误:“-[CFArray objectAtIndex:]: message sent to deallocated instance 0x3b14110”,我想不通...提前谢谢。

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowsArray 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];

cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text = [dict objectForKey:@"age"];


return cell;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0/255.0 green:207.0/255.0 blue:255.0/255.0 alpha:1.0];
self.title = NSLocalizedString(@"NOW", @"NOW");
NSURL *url = [NSURL URLWithString:@"http://10.0.1.8/~imac/iphone/jsontest.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

//  NSLog(jsonreturn); // Look at the console and you can see what the restults are

NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
    rowsArray = [dict objectForKey:@"member"];
}

NSLog(@"Array: %@",rowsArray);
    NSLog(@"count is: %i", [self.rowsArray count]);


[jsonreturn release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
    }

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


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

 @end

【问题讨论】:

  • 崩溃发生在哪一行?

标签: iphone json uitableview


【解决方案1】:

看起来rows 是一个实例变量,其中包含您要显示的数据?如果是这样,您在分配时不会保留它。记住:如果你想保留一个对象,你必须声明它的所有权。这样做的方法是要么自己分配,要么retain/copy分配在别处的对象。

这个作业

rows = [dict objectForKey:@"member"];

不这样做。这意味着rows 正在被解除分配,并最终持有对解除分配对象的引用。

另外,rowsArrayrows 有什么区别?您如何确定rowsArray 返回的countrows 相同?通常,您应该在所有 UITableViewDataSource 方法中使用相同的数据源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多