2011-03-28 59 views
0

我跟着释放自定义类对象

@interface Res : NSObject {  
    int _id; 
    NSString *_name; 
    NSString *_comments;  
// ... and to many other objects. 
}@property (nonatomic) int id; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *comments; 

在我看来,一个名为RES I类有一个UITableView,我希望用户在的UITextField内UITableViewCell的,所以我已经添加下面的代码输入值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
//code for creating cell 

switch (indexPath.row) { 
     case 0: 
      // Label 
      [[cell textLabel] setText:@"Name :"]; 

      // Textbox 
      UITextField *_txtName = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 180, 44)]; 
      [_txtName setDelegate:self]; 
      [_txtName setTag:0]; 
      [cell.contentView addSubview:_txtName]; 
      [_txtName release], _txtName = nil;    
      break; 
     case 1: 
      // ......  
} 

现在,当用户在文本框中输入值,我通过以下方法

- (void)textFieldDidEndEditing:(UITextField *)textField 
{  
    switch ([textField tag]) { 
     case 0: 
      [_resTemp setName:[textField text]]; 
      break;    
     default: 
      break; 
    } 
} 

我宣布得到它_resTemp变量作为我的.h文件实例变量
ViewDidLoad方法我写_resTemp = [[Res alloc] init];
ViewDidUnload方法我释放它像[_resTemp release];
,我也释放它的dealloc方法相同。

我仍然得到关于这个变量的内存泄漏。
我不明白在哪里发布这个对象,或者我需要改变我的逻辑。 任何人都可以给我一些指向UITableView数据输入代码的链接吗?

+0

查看发布前的预览..你的代码格式不正确 – Krishnabhadra 2011-03-28 08:22:34

回答

1

您应该在Res类中定义一个dealloc方法。

- (void)dealloc 
{ 
    [_name release]; 
    [_comments release]; 
    [super dealloc]; 
} 

该方法将释放Res对象中包含的对象,然后释放它。

+0

谢谢j_freyre,它为我工作。 – 2011-03-28 11:28:38

+0

不客气;) – 2011-03-28 11:37:57