释放自定义单元类应用程序中的对象时崩溃

时间:2012-12-01 11:14:35

标签: iphone ios ipad

我的自定义TableViewCell发布时,我的应用崩溃了。我试过的代码如下所示。它没有找到变量的名称。

Cell在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;


    }

在我的自定义单元格类中,我在dealloc方法中释放单元格对象。在我的自定义单元格中,我正在这样做

- (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];
}

2 个答案:

答案 0 :(得分:1)

如果你在表格视图单元格的dealloc中执行[sfdfbutton release],你可能会过度释放你的UIButton:sfdfbutton。那是因为它是自动释放的对象。

如果这不是问题,请尝试使用Instruments运行应用程序。

答案 1 :(得分:-1)

要解决此类问题,请在NSZombieEnabled设置为YES(产品>编辑方案...并将其添加到环境变量)的情况下启动您的应用。

这将指出哪个变量被过度释放。您还可以通过键入Cmd + Shift + B来执行程序的静态分析。

相关问题