释放自定义类对象

时间:2011-03-28 08:17:23

标签: iphone uitableview release

我有一个名为Res的课程如下

@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;  

在我的视图中我有一个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;  
    }  
}  

我在我的.h文件中将_resTemp变量声明为实例变量
ViewDidLoad方法上我写_resTemp = [[Res alloc] init];
ViewDidUnload方法上,我将其释放为[_resTemp release]; 我也在dealloc方法中以相同的方式释放它。

仍然有关于此变量的内存泄漏 我不知道在哪里发布这个对象,或者我需要改变我的逻辑。 任何人都可以给我一些链接,引用UITableView中的数据输入代码吗?

1 个答案:

答案 0 :(得分:1)

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

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

该方法将释放Res对象中包含的对象。

相关问题