我有一个UITableViewCell的子类,当它到达[super dealloc]行时会崩溃。我的单元格中有几个textField,错误消息是*** -[UITextField release]: message sent to deallocated instance 0x739dfd0
下面是相关的代码片段(我确实有其他textField,但是它们都以相同的方式处理。我怀疑是将其添加到单元格的contentView中。但是我不知道如何纠正它。
自定义UITableViewCell的.h文件:
@interface ExerciseTableViewCell : UITableViewCell {
UITextField *textField1;
}
@property (nonatomic, retain) UITextField *textField1;
@end
.m文件:
@implementation ExerciseTableViewCell
@synthesize textField1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIView *myContentView = self.contentView;
UITextField *newTextField1 = [[UITextField alloc] init];
self.textField1 = newTextField1;
[newTextField1 release];
[myContentView addSubview:textField1];
}
return self;
}
}
- (void)dealloc {
[textField1 release];
[super dealloc];
}
我看不到为什么我会多次释放textField?
最佳答案
更改:
UITextField *newTextField1 = [[UITextField alloc] init];
self.textField1 = newTextField1;
[newTextField1 release];
[myContentView addSubview:textField1];
至:
self.textField1 = [[[UITextField alloc] init] autorelease];
[myContextView addSubview:self.textField1];
关于iphone - 自定义UITableViewCell在[super dealloc]上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10674165/