我有一段时间试图弄清楚为什么我收到EXC_BAD ACCESS错误。控制台给了我这个错误:“ - [CFArray objectAtIndex:]:发送到解除分配的实例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
答案 0 :(得分:2)
看起来rows
是一个保存您想要显示的数据的实例变量?如果是这样,您在分配时不会保留它。请记住:如果您想保留一个对象,则必须声明对象的所有权。这样做的方法是自己分配它,或者retain
/ copy
在其他地方分配的对象。
此作业
rows = [dict objectForKey:@"member"];
不这样做。这意味着rows
正在被取消分配,并最终保留对已释放对象的引用。
另外,rowsArray
和rows
之间有什么区别?如何确保rowsArray
返回与count
相同的rows
?通常,您应该在所有UITableViewDataSource
方法中使用相同的数据源。