2015-08-20 21 views
0

當用戶在UITableView的文本區域單元格中輸入不正確的輸入時,我想將頁腳更改爲帶有彩色文本的警告,以描述錯誤。除了着色之外,我已經完成了它的工作。 我的代碼如下我做錯了什麼,這樣的文字沒有着色?爲什麼我無法更改桌面頁腳的文字顏色?

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    switch (section) 
    { 
     // Inspection confirms this case is entered when needed. 
     case SECTION_VALIDATABLE_FORM: 
     { 
      UITableViewHeaderFooterView *warningFooter = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:K_FOOTER_WARNING]; 
      UIColor *color = [MyColors uiColorForKey:K_COLOR_TEXT_ERROR]; // inspection confirms this is the UIColor equivalent of #BB2222 
      [warningFooter.textLabel setTextColor:color]; 
      return warningFooter; 
     } 
     // other sections... 
    } 
    return nil; 
} 

回答

0

UITableViewHeaderFooterView使用自定義的UILabel子類,顯然,顏色不能改變。

您應該繼承UITableViewHeaderFooterView並手動添加自己的標籤。

@interface XXTableViewHeaderFooterView : UITableViewHeaderFooterView 
@property (nonatomic, strong) UILabel *myLabel; 
@end 


@implementation XXTableViewHeaderFooterView 

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier; 
{ 
    self = [super initWithReuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _myLabel = [[UILabel alloc] initWithFrame:...]; 
     _myLabel.textColor = ...; 
     [self.contentView addSubview:_myLabel]; 
    } 
    return self; 
} 

@end 
+0

我可以使用類別來實現嗎? –

+0

不幸的是,你不能添加屬性的類別。 – deadbeef

+0

對不起,我很困惑...你沒有提到'@ property's,我能不能用一個重寫的getter創建一個類別?另外,我必須覆蓋哪些屬性? 'label'? –