3

I have a problem with my app crashing when my custom TableViewCell gets released. The code that i tried is given below .It Didn't go for the names of the variouble.

The Cell gets initialized like the following in cellForRowAtIndexPath.

static NSString *CellIdentifier1 = @"CellIdentifier";
    static NSString *CellIdentifier2 = @"Cell";
     if (indexPath.section == 0 && indexPath.row==0)
    { 
         CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       if (cell == nil)
        {
            cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease];
            cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"dfdfdf.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        }
   [cell.fgfbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
 cell.myImageView.image=[UIImage imageNamed:@"fdfdfdf.png"];
    cell.fddfdbutton.tag=indexPath.section+1;
       return cell;

    }
    else if (indexPath.section == 1 && indexPath.row==0) 
    {
        Customcellwithimage *cell = (Customcellwithimage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if (cell == nil)
        {
            cell = [[[Customcellwithimage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease];
             cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"fetrtrtrt.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
        }

         cell.selectionStyle = UITableViewCellSelectionStyleNone;

        [cell.dfdfbutton1 addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside]; 


        return cell;


    }

and in my custom cell class i am releasing the cell objects in the dealloc methode.and in my custom cell i am doing like this

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        nameLabel = [[UILabel alloc]init];
        [nameLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.numberOfLines = 0;

        nameLabel.backgroundColor =[UIColor clearColor];
        nameLabel.adjustsFontSizeToFitWidth = NO;

       sfdfbutton = [UIButton buttonWithType:UIButtonTypeCustom];

        [sdsdbutton setImage:[UIImage imageNamed:@"_it.png"] forState:UIControlStateNormal];
        [sdbutton setImage:[UIImage imageNamed:@"ed.png"] forState:UIControlStateSelected]; 
        sdsdbutton.selected=NO;

            myImageView = [[UIImageView alloc] init];
        bottomborder=[[UIImageView alloc] init];
        UIImage *img1 = [UIImage imageNamed:@"_bg.png"];
        [bottomborder setImage:img1];

        [self.contentView addSubview:nameLabel];

        [self.contentView addSubview:myImageView];

    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //CGRect contentRect = self.contentView.bounds;
    //CGFloat boundsX = contentRect.origin.x;
    CGRect frame;

    //frame= CGRectMake(0 ,0, 300, 135);
    //cellview.frame = frame;
    frame= CGRectMake(60 ,8, 220, 20);
    nameLabel.frame = frame;

    myImageView.frame = CGRectMake(10,6,40,40);
    NSLog(@"nameLabel frame: %@", NSStringFromCGRect(myImageView.frame));
    nhghbutton.frame =CGRectMake(4,12 + frame.origin.y + frame.size.height, 82, 35);

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];


}
- (void)dealloc {

   [bottomborder release];
   [myImageView release];
   [label release];
   [super dealloc];
}
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
angry_developer
  • 215
  • 1
  • 11

2 Answers2

1

You are probably overreleasing your UIButton: sfdfbutton if you do [sfdfbutton release] in dealloc of your table view cell. That's because it is autoreleased object.

If this is not the problem, try running application with Instruments.

Piotr Wach
  • 913
  • 2
  • 9
  • 22
-1

To solve these kinds of issues, start your app with NSZombieEnabled set to YES (Product > Edit Scheme... and add this to Environment Variables).

This will point out exactly which variable gets overreleased. You can also perform a static analysis of your program by typing Cmd+Shift+B.

Krumelur
  • 31,081
  • 7
  • 77
  • 119