【问题标题】:Why can't I change my table footer's text color?为什么我不能更改表格页脚的文本颜色?
【发布时间】:2015-08-20 15:32:19
【问题描述】:

当用户在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;
}

【问题讨论】:

    标签: ios uitableview cocoa-touch


    【解决方案1】:

    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
    

    【讨论】:

    • 我可以使用类别来完成这个吗?
    • 很遗憾,您不能添加具有类别的属性。
    • 对不起,我很困惑......你没有提到@propertys,我可以不只创建一个覆盖getter的类别吗?另外,我必须覆盖什么属性? label?
    • 正如我所说的 UITableViewHeaderFooterView 使用 UILabel 的私有子类,类别将不起作用。只需创建一个子类,我添加了样板代码。
    • 那么你现在需要在 tableView:viewForFooterInSection: 中使用 myLabel 而不是 textLabel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2020-02-29
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多