问题描述
|
我有3个层次结构的导航应用程序。我的第一级是表格视图控制器。该控制器的nib文件为\“ 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
子类别
#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
我不知道为什么释放我的对象。有人能帮我吗。
编辑:添加了子类别代码
解决方法
这是问题所在:
NSMutableArray *namesArray = [[NSMutableArray alloc] initWithCapacity:[subCategories count]];
NSMutableArray* idArray = [[NSMutableArray alloc] initWithCapacity:[subCategories count]];
...
subCategoryId = [NSArray arrayWithArray:idArray];
subCategoryName = [NSArray arrayWithArray:namesArray];
[idArray release];
[namesArray release];
此时,subCategoryId
和subCategoryName
被自动释放。这意味着parseJSON:
方法完成后将无法访问它们。您有两种选择:
// Because idArray and namesArray are already retained (you alloc\'d them)
subCategoryId = idArray;
subCategoryName = namesArray;
要么:
...
// this is equivalent to what you have now,except these two will be retained.
// this is different from the above because subCategoryId and subCategoryName are now NSArrays,whereas above they were NSMutableArrays.
subCategoryId = [idArray copy];
subCategoryName = [namesArray copy];
...
, SubCategory类别的范围仅是您的viewDidLoad。制作浅表副本时,它/或者该副本在范围之外不可用,将其分配给subCategories时,需要保留它
subCategories = [categories retain];
另外,您完全可以放弃使用SubCategory类别。而是:
subCategories = [[SubCategory alloc] init];
[subCategories parseJSON:jsonArray]; }