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

fra*_*yan 6 objective-c uitableview ios uiappearance

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

这是我的代码,其中的注释显示了什么对我有用,什么不对:

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
Run Code Online (Sandbox Code Playgroud)

如您所见,第二种方式是工作,但我不能为普通文本和详细文本设置不同的颜色.

有什么我做错了吗?

PS在Interface Builder中静态定义东西对我来说不起作用 - 我有可以在运行时动态更改的主题.

Two*_*aws 4

您正在做正确的事情,但是 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];
}
Run Code Online (Sandbox Code Playgroud)

或者,您可能还会发现这个答案还有一些其他想法。