У меня есть навигационное приложение с 3 уровнями иерархии. Мой первый уровень это контроллер представления таблиц. Файл пера этого контроллера - "TableView". У меня проблема здесь. Мой код такой:
RootViewController
#import "RootViewController.h"
#import "SubCategory.h"
#import "OffersViewController.h"
@implementation RootViewController
@synthesize subCategories;
@synthesize offersView;
- (void)dealloc
{
NSLog(@"root dealloc");
//[subCategories release];
[super dealloc];
}
- (void)viewDidLoad
{
NSLog(@"root view load");
[super viewDidLoad];
self.title = @"Sub Categories";
NSString *jsonArray = [NSString stringWithFormat:@"{ "
@" \"sub-categories\": { "
@" \"parent\": \"1\", "
@" \"count\": \"2\", "
@" \"sub-category\": [{ "
@" \"id\": \"1\", "
@" \"name\": \"Buy\" "
@" }, "
@" { "
@" \"id\": \"2\", "
@" \"name\": \"Sell\" "
@" }] "
@" } "
@" }"];
SubCategory* categories = [[SubCategory alloc] init];
subCategories = categories;
[subCategories parseJSON:jsonArray];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"root sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"root numberOfRows");
return [subCategories.subCategoryName count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"root didSelectRow");
OffersViewController * offers = [[OffersViewController alloc] initWithNibName:@"OffersView" bundle:nil];
offersView = offers;
// I have exception on this row. exception is:
//-[__NSArrayI objectAtIndex:]: message sent to deallocated instance 0x4b04c00
offersView.title = [NSString stringWithFormat:@"%@", [subCategories.subCategoryName objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:offersView animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"root cellForRow");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] init] autorelease];
}
cell.textLabel.text = [subCategories.subCategoryName objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end
SubCategory
#import "SubCategory.h"
#import "JSON.h"
@implementation SubCategory
@synthesize parentId;
@synthesize count;
@synthesize subCategoryId;
@synthesize subCategoryName;
- (void) parseJSON: (NSString*) jsonArray {
NSDictionary *results = [jsonArray JSONValue];
NSString *parent = (NSString*) [[results objectForKey:@"sub-categories"] objectForKey:@"parent"];
parentId = [parent intValue];
NSString *cnt = (NSString*) [[results objectForKey:@"sub-categories"] objectForKey:@"count"];
self.count = [cnt intValue];
NSDictionary *subCategories = [[results objectForKey:@"sub-categories"] objectForKey:@"sub-category"];
NSMutableArray *namesArray = [[NSMutableArray alloc] initWithCapacity:[subCategories count]];
NSMutableArray* idArray = [[NSMutableArray alloc] initWithCapacity:[subCategories count]];
for (NSDictionary *subCategory in subCategories) {
[idArray addObject:[subCategory objectForKey:@"id"]];
[namesArray addObject:[subCategory objectForKey:@"name"]];
}
subCategoryId = [NSArray arrayWithArray:idArray];
subCategoryName = [NSArray arrayWithArray:namesArray];
[idArray release];
[namesArray release];
//[parent release];
//[cnt release];
}
@end
Я не знаю, почему мой объект выпущен. Может ли кто-нибудь помочь мне.
РЕДАКТИРОВАТЬ: Добавлен код подкатегории