开发者

Releasing custom class object

I have a class called Res as follow

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

On my view I have a UITableView, I want user to enter values in UITextfield inside UITableViewCell so I have added below code

- (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:  
          // ......    
}  

Now when user enter values in textbox I get it via following method

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

I declared _resTemp variable as instance variable in my .h file

on ViewDidLoad method I write _resTemp = [[Res alloc] init];

and on ViewDidUnload method I release it like [_resTemp release];

and I also release it same way 开发者_Python百科in dealloc method.

Still I get memory leaks regarding this variable.

I do not get where to release this object or do I need to change my logic. Can anyone give me some links that refers to code for data entry in UITableView?


You should define a dealloc method in your Res class.

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

The method will release objects contained in your Res object before releasing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜