Ios 使用UIAppearance更改所有UITableViewCell的文本颜色

Ios 使用UIAppearance更改所有UITableViewCell的文本颜色,ios,objective-c,uitableview,uiappearance,Ios,Objective C,Uitableview,Uiappearance,我无法使用UIAppearance机制更改UITableViewCell的文本颜色 以下是我的代码和注释,说明什么对我有效,什么对我无效: UITableViewCell *cell = [UITableViewCell appearance]; cell.backgroundColor = [UIColor blueColor]; // working cell.textLabel.textColor = [UIColor whiteColor]; // NOT WORKING cell.de

我无法使用
UIAppearance
机制更改
UITableViewCell
的文本颜色

以下是我的代码和注释,说明什么对我有效,什么对我无效:

UITableViewCell *cell = [UITableViewCell appearance];
cell.backgroundColor = [UIColor blueColor]; // working
cell.textLabel.textColor = [UIColor whiteColor]; // NOT WORKING
cell.detailTextLabel.textColor = [UIColor redColor]; // NOT WORKING

UILabel *cellLabel = [UILabel appearanceWhenContainedIn:[UITableViewCell class], nil];
cellLabel.textColor = [UIColor whiteColor]; // working
正如您所看到的,第二种方法是可行的,但我无法为普通文本和详细文本设置不同的颜色

我做错什么了吗


另外,在Interface Builder中静态定义内容对我来说不起作用-我有可以在运行时动态更改的主题。

你做得对,但iOS无法使用
UIAppearance
区分这两个标签。您应该在
willDisplayCell
中设置文本颜色,或者,如果您确实想使用
UIAppearance
,请创建自定义
UILabel
子类,以便更准确地定位

如果要使用willDisplayCell,它将如下所示:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textColor = [UIColor redColor];
}

或者,您可以这样设置文本颜色

[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.detailTextLabel setTextColor:[UIColor redColor]];

正如我在帖子中提到的,它不起作用。我猜
[cell.textlab setTextColor:[UIColor whiteColor]]之间没有区别
cell.textlab.textColor=[UIColor whiteColor];//不工作
您可以尝试从视图UILabel*label=(UILabel*)[cell.contentView viewWithTag:yourLabelTag]获取标签;然后设置文本颜色。它是一个外观代理,没有任何contentView。我需要一个小说明-我只为详细文本创建了一个UILabel子类,比如MyLabel。然后,我尝试为UILabel外观和MyLabel外观着色,使两者具有相同的类层次结构。问题是UILabel颜色总是“获胜”,而MyLabel颜色永远不会应用。对如何评估这些条件有何想法?