【问题标题】:Custom table view cell selection font color自定义表格视图单元格选择字体颜色
【发布时间】:2012-05-26 15:25:07
【问题描述】:

我有一个自定义UITableViewCell。它内部有 3 个自定义标签和自定义文本。

当我点击单元格时,我希望所有这些标签的 textColor 变为白色。就像电子邮件应用 UITableViewCell 行为一样。

为此,我在自定义单元格类中编写了这个。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

    if (self.selected) {
        _subjectLabel.textColor = [UIColor whiteColor];
        _messageLabel.textColor = [UIColor whiteColor];
        _usernameLabel.textColor = [UIColor whiteColor];
    }else {
        _subjectLabel.textColor = [UIColor blackColor];
        _messageLabel.textColor = [UIColor grayColor];
        _usernameLabel.textColor = [UIColor blackColor];
    }



}

我能够得到它。但它不像在电子邮件应用程序中那样流畅。颜色仅在一小段延迟后发生变化。我应该重写 UITableViewCell 的哪个方法来放入此代码。我知道以下选项,但它们不会将行为赋予自定义单元格中的自定义标签。

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

【问题讨论】:

    标签: ios ios5 uitableview


    【解决方案1】:

    设置标签的高亮文本颜色,这一切都会自动为您完成。您根本不需要在 setSelected 中做任何特别的事情。

    例如

    _subjectLabel.highlightedTextColor = [UIColor whiteColor];
    

    【讨论】:

    • 小提醒,如果单元格是自定义单元格,可以打开xib,在Attributes Inspector下,可以设置“Highlighted”颜色。
    【解决方案2】:

    当我们选择UITableView的任何单元格时,立即调用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,你可以使用这个。

    【讨论】:

    • -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 仅在您将手指从单元格中取出时才会触发.. 当您保持在单元格上时不会触发.. 当我将手指放在单元格上时,我希望颜色会发生变化。 . 以电子邮件应用为例
    • 在这种情况下,您可以使用 touchBegan 方法,只要您将手指放在单元格上,就会调用此方法,目前您使用的是哪种方法?
    • 你用什么方法来调用这个?
    • 在导航控制器中,当用户回到表格时,蓝色应该淡出,标签文本应该再次变黑...
    • 单元格调用这个- (void)setSelected:(BOOL)selected animated:(BOOL)animated方法
    猜你喜欢
    • 2012-07-10
    • 2017-06-25
    • 2011-04-27
    • 2015-09-10
    • 2020-09-11
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多