При загрузке таблицы JSON и Iphone возникает ошибка EXC_BAD_ACCESS при прокрутке - PullRequest
0 голосов
/ 01 июня 2010

У меня немного времени, чтобы понять, почему я получаю ошибку 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

1 Ответ

2 голосов
/ 01 июня 2010

Похоже, rows это переменная экземпляра, которая содержит данные, которые вы хотите отобразить? Если это так, вы не сохраняете его при назначении. Помните: если вы хотите сохранить объект, вы должны претендовать на владение им. Способ сделать это - выделить его самостоятельно или retain / copy объект, размещенный в другом месте.

Это задание

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

этого не делает. Это означает, что rows освобождается и в конечном итоге содержит ссылку на освобожденный объект.

Кроме того, в чем разница между rowsArray и rows? Как вы можете быть уверены, что rowsArray возвращает тот же count, что и rows? Как правило, вы должны использовать один и тот же источник данных во всех методах UITableViewDataSource.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...