2011-07-02 4 views
2

3 개의 UILabel을 포함하는 사용자 정의 셀이 있습니다. 마지막 레이블에는 해당 셀의 데이터가 한 줄에 하나씩 나열되도록 \ 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 변수를 전달한 다음이를 사용하여 레이블의 높이를 설정합니다.

관련 문제