gpt4 book ai didi

ios - 使用 UIAppearance 更改所有 UITableViewCells 的文本颜色

转载 作者:可可西里 更新时间:2023-11-01 03:55:44 25 4
gpt4 key购买 nike

我无法使用 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];
}

或者,you might also find this answer has some other ideas.

关于ios - 使用 UIAppearance 更改所有 UITableViewCells 的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com