UITableViewCell突出显示更改文本字段颜色

时间:2012-12-12 14:37:41

标签: iphone ios cocoa-touch

我已经将UITableViewCell子类化,我有以下代码:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (selected)
        self.textField.textColor = [UIColor whiteColor];
    else
        self.textField.textColor = [UIColor blackColor];

    [super setSelected:selected animated:animated];
}

基本上我的单元格中只有一个UITextField,但标签的颜色不会自动变为白色,所以我需要一种方法来改变它。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您应该使用cell.textLabel.highlightedTextColor并且未在setSelected:

中更改它

答案 1 :(得分:0)

我相信这是在UITextField上设置文字颜色的正确方法。但我想知道的是:self.textField是否正确连接到正确的实例? (通过Interface Builder / StoryBoard或代码中)

尝试在setSelected方法的顶部添加此代码,并在运行应用时查看控制台:

NSLog(@"self.textField = %@", self.textField);

答案 2 :(得分:0)

在你的cellForRowAtIndexPath

cell.selectionStyle = UITableViewCellSelectionStyleNone;

并且在你的子视图中有类似的东西

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

if (selected)
        self.textLabel.textColor=[UIColor blackColor];
    else
        self.textLabel.textColor=[UIColor greenColor];


}