iOS:使用UIAppearance定义自定义UITableViewCell颜色
如果在分组的UITableViewCell上设置backgroundColor属性,则背景颜色会成功更改。大。iOS:使用UIAppearance定义自定义UITableViewCell颜色
但是我想用UIAppearance改变我所有的UITableViewCells的背景颜色,所以我可以在一个地方做到这一点,并影响到处都有变化。这里是我的代码:
[[UITableViewCell appearance] setBackgroundColor:[UIColor colorWithRed:30.0/255.0 green:30.0/255.0 blue:30.0/255.0 alpha:1.0]];
的UITableViewCell实现UIAppearance和UIAppearanceContainer,所以我还以为这会工作。但事实并非如此。我也试过使用-[UITableViewCell appearanceWhenContainedIn:(Class)]
,这也不起作用。
任何想法?
更新(2013/7/8) - 这已在更新版本的iOS中修复。但是,如果您的目标是iOS 6或更低版本,则值得了解。
你可以责怪苹果为这一个,它实际上很不重要。 技术上,backgroundColor
通过外观代理不可定制。
从苹果公司的文档:
为了支持外观定制,类必须符合UIAppearanceContainer协议和相关的访问方法必须注明UI_APPEARANCE_SELECTOR。所以
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
,因为它打上我们知道它的工作原理与
UIAppearance
的UI_APPEARANCE_SELECTOR
标签:
如果我们进入一类像UIBarButtonItem
,并期待在tintColor
属性我们可以看到这一点。
这里就是苹果的特别意思是:在UIView
,backgroundColor
没有外观选择标签,但UIAppearance
仍然有效。根据苹果提供的所有文件不应该,但它确实!
这给人以误导的印象,它将适用于UIView
的所有子类别,包括UITableView
。这已经到来之前,in this previous SO answer
因此,底线是,backgroundColor
不应UIAppearance
在所有的工作,但由于某种原因它在UIView
。不保证在UIView
子类上工作,并且它根本不能在UITableView
上工作。对不起,我无法给你一个更积极的答案!
您可以创建自己的符合UIAppearance的UITableViewCell的子类,并用UI_APPEARANCE_SELECTOR标记自定义设置器。然后在自定义设置器的超类上设置单元格backgroundColor。
在你的appDelegate
[[CustomCell appearance] setBackgroundCellColor:[UIColor redColor]];
在你的UITableView子类
@interface CustomCell : UITableViewCell <UIAppearance>
@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR;
@implementation CustomCell
@synthesize backgroundCellColor;
-(void)setBackgroundCellColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
}
我在这个例子中使用ARC。
即使没有子类化,也可以使用类别和自定义属性来代理外观调用。如果您想为不能使用子类的部分进行主题化(例如,对于'UIPrintInteractionController'),它会很有用。 – Tom
我用这个分类。下面是示例代码 在您的.h文件中写
*@interface UITableViewCell (MyCustomCell)
@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR;
@end*
在您.m文件写
*@dynamic backgroundCellColor;
-(void)setBackgroundCellColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
}*
它的工作很适合我。 :) 谢谢奈特!
没有子类!在子类上执行此操作可能不是最佳实践,特别是如果要打击所有tableView背景。这是很多子类。很多潜在的错误。真是一团糟。最好的办法是使用一个类别。你将不得不为tableViewCell和tableView设置一个。我将只展示一个单元格。您必须执行的tableView属性是backgroundColor属性。 NB。我正在将我的方法预先添加为“sat”。
// .H
#import <UIKit/UIKit.h>
@interface UITableViewCell (Appearance)<UIAppearance>
@property (strong, nonatomic) UIColor *satBackgroundColor UI_APPEARANCE_SELECTOR;
@end
// .M
#import "UITableViewCell+Appearance.h"
@implementation UITableViewCell (Appearance)
- (UIColor *)satBackgroundColor
{
return self.backgroundColor;
}
- (void)setSatBackgroundColor:(UIColor *)satBackgroundColor
{
self.backgroundColor = satBackgroundColor;
}
@end
现在在你的appDelegate或您将导入的类别,只需要调用它,就好像它有一个外观的一些经理级代理内置。
UITableViewCell *cell = [UITableViewCell appearance];
cell.satBackgroundColor = [UIColor orangeColor];
好的,所以现在只需做一个tableView的背景属性。简单。
感谢您的解释。因为这个原因,我可能会花几个小时在墙上点头。 – matsr
是的,很好的答案。谢谢! –
如果这真的让你感到困扰(它让我困扰!),那么肯定会向苹果公司提交一份错误报告。我不认为这本身就是一个“错误”,但这绝对是一个缺陷。我想我甚至可能会看到苹果的某个人在代码演示中使用'setBackgroundColor'作为外观代理。 – lxt