2011-03-25 31 views
0

我有點堅持想知道爲什麼,當我使用這個自定義的UIView,顯示的3×UILabels一排,,雖然我看到紅色的輪廓的框架,我沒有看到框架內的任何東西。這是標籤不出現,也沒有黑邊的我已經設定的UILabels。爲什麼會發生這種自定義的UIView碼不顯示UILabel的?

任何人都可以在這裏發現的問題嗎?

#import <UIKit/UIKit.h> 
@interface LabelRowView : UIView { 
    UILabel *_dateLabel; 
    UILabel *_titleLabel; 
    UILabel *_locationLabel; 
} 
@property (nonatomic, retain) UILabel *dateLabel; 
@property (nonatomic, retain) UILabel *titleLabel; 
@property (nonatomic, retain) UILabel *locationLabel; 
- (void)setFont:(UIFont *)newFont; 
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr; 
@end 

// ============================================================== 

@implementation LabelRowView 
    @synthesize dateLabel = _dateLabel; 
    @synthesize titleLabel = _titleLabel; 
    @synthesize locationLabel = _locationLabel; 

- (void)setDefaultsForLabel:(UILabel*)label { 
    label.layer.borderColor = [UIColor blackColor].CGColor; 
    label.layer.borderWidth = 1.0; 
    label.textAlignment = UITextAlignmentLeft; 
    label.lineBreakMode = UILineBreakModeTailTruncation; 
    label.opaque = YES; // TODO: put back in for performance reasons if possible 
    label.backgroundColor = [UIColor clearColor]; 
} 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.dateLabel  = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];  // Position will be updated later 
     self.titleLabel  = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];  // Position will be updated later 
     self.locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];  // Position will be updated later 

     // Set Defaults 
     [self setDefaultsForLabel:self.dateLabel]; 
     [self setDefaultsForLabel:self.titleLabel]; 
     [self setDefaultsForLabel:self.locationLabel]; 

     self.layer.borderColor = [UIColor redColor].CGColor; 
     self.layer.borderWidth = 1.0; 
    } 
    return self; 
} 

- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr { 
    self.dateLabel.text = dateStr; 
    self.titleLabel.text = titleStr; 
    self.locationLabel.text = locationStr; 
} 

- (void)setFont:(UIFont *)newFont { 
    self.dateLabel.font = newFont; 
    self.titleLabel.font = newFont; 
    self.locationLabel.font = newFont; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
     // Get current width for this subview 
     CGFloat contentViewWidth = self.frame.size.width; 
     CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999); 
     CGFloat oneLineHeight = [@"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height; 

     // Calculate Widths 
     CGFloat dateColumnWidth = [@"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width; 
     CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0; 
     CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth; 

     // Date - Layout 
     CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight); 
     self.dateLabel.frame = newRect; 

     // Date - Layout 
     newRect = CGRectMake(dateColumnWidth, 0.0, titleColumnWidth, oneLineHeight); 
     self.titleLabel.frame = newRect; 

     // Title - Layout 
     newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight); 
     self.locationLabel.frame = newRect; 
} 

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

@end 

回答

5

你似乎並不被添加UILabels作爲子視圖。

+0

DOH - 感謝Arkku我一直俯瞰着這個,因爲我會依然古色古香期待通過它 - 檢查所有的addSubview從根控制器向下穿過hieracy下來,但忘了就在葉節點之一:) – Greg 2011-03-25 01:51:18

相關問題