2011-07-22 35 views
1

我繼承了TTTableMessageItemCell,得到了EXC_BAD_ACCESS運行時錯誤。 Anythign錯了?子類TTTableMessageItemCell,有什麼不對嗎?

CustomTTTableSubtitleItemCell.h

#import "Three20/Three20.h" 
@interface CustomTTTableSubtitleItemCell : TTTableMessageItemCell { 
    TTButton *_rightButton; 
} 

@end 

CustomTTTableSubtitleItemCell.m

#import "CustomTTTableSubtitleItemCell.h" 
#import "CustomTTTableSubtitleItem.h" 
#import "XYDefaultStyleSheet.h" 
/////////////////////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////////////////////////////// 

static CGFloat kHPadding = 10; 
static CGFloat kVPadding = 15; 


@interface ButtonStyleSheet : TTDefaultStyleSheet 
@end 

@implementation ButtonStyleSheet 

- (TTStyle*)blueToolbarButton:(UIControlState)state { 
    TTShape* shape = [TTRoundedRectangleShape shapeWithRadius:4.5]; 
    UIColor* tintColor = RGBCOLOR(30, 110, 255); 
    return [TTSTYLESHEET toolbarButtonForState:state shape:shape tintColor:tintColor font:nil]; 
} 

@end 


@implementation CustomTTTableSubtitleItemCell 


+ (CGFloat)tableView:(UITableView*)tableView rowHeightForItem:(id)item { 
    CustomTTTableSubtitleItem* captionedItem = item; 

    CGFloat maxWidth = tableView.width - kHPadding*2; 

    CGSize titleSize = [captionedItem.title sizeWithFont:TTSTYLEVAR(myTitleFont) 
            constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX) 
             lineBreakMode:UILineBreakModeWordWrap]; 

    CGSize textSize = [captionedItem.text sizeWithFont:TTSTYLEVAR(myHeadingFont) 
            constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX) 
             lineBreakMode:UILineBreakModeWordWrap]; 
    CGSize subtextSize = [captionedItem.caption sizeWithFont:TTSTYLEVAR(mySubtextFont) 
              constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

    return kVPadding*2 + titleSize.height + textSize.height + subtextSize.height + kVPadding; 
} 




- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier { 
    if (self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier]) { 
     _item = nil; 
     [TTStyleSheet setGlobalStyleSheet:[[[ButtonStyleSheet alloc] init] autorelease]]; 


    } 
    return self; 
} 


- (void)layoutSubviews { 
    [super layoutSubviews]; 

    [self.detailTextLabel sizeToFit]; 
    self.detailTextLabel.top = kVPadding; 

    self.textLabel.height = self.detailTextLabel.height; 

    //_rightButton.frame = CGRectMake(20, self.detailTextLabel.bottom + kVPadding, kImageWidth, kImageHeight); 

     //_rightButton.alpha = !self.showingDeleteConfirmation; 
     [_rightButton sizeToFit]; 
     _rightButton.left = self.contentView.width - (_timestampLabel.width + kHPadding); 
     _rightButton.top = self.height/2; 



} 

- (id)object { 
    return _item; 
} 

- (void)setObject:(id)object { 
    if (_item != object) { 
     [super setObject:object]; 

     CustomTTTableSubtitleItem* item = object; 


     //self.textLabel.textColor = TTSTYLEVAR(myHeadingColor); 
//  self.textLabel.font = TTSTYLEVAR(myHeadingFont); 
//  self.textLabel.textAlignment = UITextAlignmentRight; 
//  self.textLabel.contentMode = UIViewContentModeCenter; 
//  self.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
//  self.textLabel.numberOfLines = 0; 
//  
//  self.detailTextLabel.textColor = TTSTYLEVAR(mySubtextColor); 
//  self.detailTextLabel.font = TTSTYLEVAR(mySubtextFont); 
//  self.detailTextLabel.textAlignment = UITextAlignmentLeft; 
//  self.detailTextLabel.contentMode = UIViewContentModeTop; 
//  self.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 


     _rightButton = [TTButton 
         buttonWithStyle:@"blueToolbarButton:" title:item.rightButtonTitle]; 




    } 
} 

- (void)dealloc { 
    TT_RELEASE_SAFELY(_rightButton); 
    [super dealloc]; 
} 

@end 

回答

0

您使用自動釋放,當你在你的dealloc功能釋放它創建TTButton。所以釋放池和dealloc都試圖釋放你的_rightButton TTButton。

在你的頭文件,嘗試添加:

@property (nonatomic, readonly, retain) TTButton*  rightButton; 

,然後用他的get函數在源文件中創建TTButton:

/////////////////////////////////////////////////////////////////////////////////////////////////// 
- (TTButton*)rightButton { 
if (!_rightButton) { 
    _rightButton = [[TTButton 
        buttonWithStyle:@"blueToolbarButton:" title:item.rightButtonTitle] retain]; 

    [self.contentView addSubview:rightButton]; 
    } 
return rightButton; 
} 

當使用rightButton,確保使用的自.rightBotton而不是_rightButton,比如在佈局函數中(因爲你需要創建對象)。

self.rightButton.frame = CGRectMake(20, self.detailTextLabel.bottom + kVPadding, kImageWidth, kImageHeight); 

我建議打開Three20UI/TTTableMessageItemCell.h &源文件,並試圖複製的要素之一的行爲。這就是我所做的。