2011-07-02 49 views
2

我有一個包含3個UILabels的自定義單元格。最後一個標籤將包含嵌入\ n的文本,以便該單元格中的數據每行列出一個項目。我知道所需的線數,我知道每條線需要15個像素高。我必須改變這種標籤的大小的代碼是在這裏:自定義單元格中的UILabel不會動態增加高度

//Set up the cell 
static NSString *CellIdentifier = @"Cell"; 
CustomHistoryCell *cell = (CustomHistoryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[CustomHistoryCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 
cell.probeInfo.lineBreakMode = UILineBreakModeWordWrap; 
cell.probeInfo.numberOfLines = 0; 
cell.probeInfo.text = theProbes; 
[cell.probeInfo setAdjustsFontSizeToFitWidth:YES]; 
CGRect newFrame = cell.probeInfo.frame; 
newFrame.size.height = nAdditionalLabelHeight*15; 
cell.probeInfo.frame = newFrame; 

theProbes包含與\ n的文本和nAdditionalLabelHeight所包含的行數。我已經將背景顏色設置爲灰色,以便我可以看到標籤的大小是否正確,並且不會更改它的高度。任何人都可以看到我做錯了什麼?

這裏的自定義單元代碼的.h文件中的情況下,可以幫助:

@interface CustomHistoryCell : UITableViewCell { 
    UILabel *_stokerName; 
    UILabel *_smokeDateTime; 
    UILabel *_probeInfo; 

} 
@property (nonatomic,retain) UILabel *stokerName; 
@property (nonatomic,retain) UILabel *smokeDateTime; 
@property (nonatomic,retain) UILabel *probeInfo; 

@end 

而這裏的的情況下,自定義單元格的.m文件中的代碼,可以幫助:

#import "CustomHistoryCell.h" 


@implementation CustomHistoryCell 

@synthesize stokerName = _stokerName; 
@synthesize smokeDateTime = _smokeDateTime; 
@synthesize probeInfo = _probeInfo; 

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
    { 
     // Initialization code 
     self.stokerName = [[UILabel alloc]init]; 
     self.stokerName.textAlignment = UITextAlignmentLeft; 
     self.stokerName.font = [UIFont boldSystemFontOfSize:20]; 
     self.smokeDateTime = [[UILabel alloc]init]; 
     self.smokeDateTime.textAlignment = UITextAlignmentLeft; 
     self.smokeDateTime.font = [UIFont systemFontOfSize:12]; 
     self.probeInfo = [[UILabel alloc]init]; 
     self.probeInfo.textAlignment = UITextAlignmentLeft; 
     self.probeInfo.font = [UIFont systemFontOfSize:12]; 
     self.probeInfo.backgroundColor = [UIColor grayColor]; 
     [self.contentView addSubview:self.stokerName]; 
     [self.contentView addSubview:self.smokeDateTime]; 
     [self.contentView addSubview:self.probeInfo]; 
    } 


    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 
    frame= CGRectMake(boundsX+5,5, 300, 25); 
    self.stokerName.frame = frame; 
    frame= CGRectMake(boundsX+5,30, 300, 15); 
    self.smokeDateTime.frame = frame; 
    frame= CGRectMake(boundsX+5,45, 300, 15); 
    self.probeInfo.frame = frame; 
} 

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

@end 
+0

我刪除了我昨天離開的答案,希望能吸引更好的答案(即正確答案)。我唯一能想到的另一件事是'setAdjustsFontSizeToFitWidth:'只在'numberOfLines' = 1時才起作用,但即使如此,我也不確定爲什麼或者如何影響UILabel的幀大小。 –

回答

0

我想出了一個辦法來解決這個問題。我基本上覆蓋了initWithStyle並在nAdditionalLabelHeight變量中傳遞,然後用它來設置標籤的高度。

相關問題