2011-03-28 57 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