私は Objective-C を初めて使用しますが、Car オブジェクトの配列内の NSString オブジェクトが解放されたように見える理由がわかりません。
ここに私の Car.m クラスがあります:
#import "Car.h"
@implementation Car
@synthesize categoryId;
- (id)initWithPrimaryKey:(NSInteger)pk categoryId:(NSNumber *)catId carName:(NSString *)n {
if (self = [super init]) {
primaryKey = pk;
categoryId = catId;
name = n;
}
return self;
}
- (void)dealloc {
[name release];
[categoryId release];
[super dealloc];
}
- (NSInteger)primaryKey {
return primaryKey;
}
- (NSString *)name {
return name;
}
- (void)setName:(NSString *)aString {
if ((!name && !aString) || (name && aString && [name isEqualToString:aString])) return;
[name release];
name = [aString copy];
}
@end
そして、これが私の Simple_TableViewController.m クラスです。listData インスタンス変数は、viewDidLoad() で正しく設定されます。デバッガーでは、各配列要素の NSString *name プロパティはそのままです。次に、他のすべての方法で、listData が破損しています。すべての Car 要素に正しい primaryKey 値と categoryId 値が含まれていますが、NSString *name プロパティは「無効」です。
#import "Simple_TableViewController.h"
@implementation Simple_TableViewController
- (void)viewDidLoad {
NSMutableArray *array = [[NSMutableArray alloc] init];
Database *database = [Database instance];
array = [database getAllCars];
[self setListData:array];
[array release];
}
- (NSMutableArray *)listData {
return listData;
}
- (void)setListData:(NSMutableArray *)newListData {
if (listData != newListData) {
[listData release];
listData = [newListData mutableCopy];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listData release];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
Car *car = [listData objectAtIndex:row];
cell.text = car.name;
cell.font = [UIFont boldSystemFontOfSize:17];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
if (row == 0) {
return nil;
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", [[listData objectAtIndex:row] name]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
[message release];
[alert release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35;
}
@end
私はゾンビこれをいじり、指が落ちるまでそれを保持します. listData 変数の @property (nonatomic、retain) と @synthesize を削除しましたが、まだ同じ問題があります。
あなたが提供しなければならないアドバイスを事前にありがとう!