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

标签 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];
}

最佳答案

如果您在 TableView 单元格的释放中执行 [sfdfbutton release],则您可能过度释放了 UIButton: sfdfbutton。那是因为它是自动释放的对象。

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

关于iphone - 释放自定义单元格类应用程序中的对象时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658861/

相关文章:

iphone - UIWebView 间歇性地拒绝访问 html5 数据库存储

ios - Apple 的 iPhone 磁力计校准工作正常吗?

ios - 临时 AIR 应用程序安装,没有 UDID

ios - 以编程方式获取iPad设备信息

iphone - 在 iPhone 和 iPad 上使用 Quartz 绘制阴影速度很慢。其他方式?

iphone - 动画移动和旋转 UIView 不太有效

ios - 重新定位/调整 UIBezierPath 的大小

ios - UITableViewCell subview 不遵守自动布局约束

iPhone - 删除新导航

objective-c - 是否有工具可以确定我的部署目标应该是什么最低操作系统?