jay
jay

Reputation: 3667

Dealloc Method in Custom Table View Cell

I had created a custom TableViewCell for a TableView, But the app crashes if there is a dealloc method(in Custom Cell Class). Please see the below code used for table cell class :

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell {

    UILabel *nameLabel_;
    UILabel *dateLabel_;

}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;

@end


#import "CustomTableCell.h"

@implementation CustomTableCell

@synthesize nameLabel = nameLabel_;
@synthesize dateLabel = dateLabel_;


- (void) dealloc {

    [self.nameLabel release];
    self.nameLabel = nil;
    [self.dateLabel release];
    self.dateLabel = nil;

    [super dealloc];
}

@end

Code for Creating Custom Cell(cellForRowAtIndexPath):

UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCell"];

if (Cell == nil){

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[CustomTableCell class]])
        {
            Cell = (CustomTableCell *)currentObject;
            break;
        }
    }
}

If I remove the dealloc method from custom Cell, everything works Fine. Other wise I will get an exception(When I Scroll the table View) : -[CALayer release]: message sent to deallocated instance 0x6fbc590*

Does we don't need a dealloc method in customTableViewCell? Please help me to find a solution for this problem.

Upvotes: 0

Views: 599

Answers (1)

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

On the face of it, you're writing your -dealloc method incorrectly. Do it this way:

- (void) dealloc {

    [nameLabel_ release];
    nameLabel_ = nil;
    [dateLabel_ release];
    dateLabel_ = nil;

    [super dealloc];
}

You should never use your accessors in your -dealloc method; work directly with your ivars.

Upvotes: 1

Related Questions