UIAppearance没有造型的定制UITableViewCell

问题描述:

这是一种奇怪的问题!我使用UIAppearance API来设计我的UITableViewCells。我使用下面的代码来设计UITableViewCell的样式。UIAppearance没有造型的定制UITableViewCell

// table cell 
    id tableCellAppearance = [UITableViewCell appearance]; 
    [tableCellAppearance setBackgroundView:[theme imageViewForTableViewCellBackground]]; 

// table cell 
-(UIImageView *) imageViewForTableViewCellBackground 
{ 
    UIImageView *backgroundView = [[UIImageView alloc] init]; 
    [backgroundView setImage:[UIImage imageNamed:@"tile_heading_background"]]; 

    return backgroundView; 
} 

当我运行应用程序,看到UITableViewCells只有1个单元格在UITableView中的样式。中间的单元格。这是为什么?

+0

在哪里调用此方法imageViewForTableViewCellBackground? – nsgulliver 2013-03-06 21:13:51

+0

只是一个帮助器方法来获取UIImageView设置为UITableViewCell的背景。上述代码仅在App委托中调用一次。我相信问题在于单元格是动态生成的,而外观API不支持将样式应用到动态生成的控件。 – 2013-03-06 21:14:56

+2

为什么不实现一个CustomTableViewCell? – nsgulliver 2013-03-06 21:15:57

创建CustomUITableViewCell贯彻<UIAppearance>协议

CustomCell.h

@interface CustomCell : UITableViewCell <UIAppearance> 

@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR; 

CustomCell.m

@implementation CustomCell 

@synthesize backgroundCellColor; 

-(void)setBackgroundCellColor:(UIColor *)backgroundColor 
{ 
    [super setBackgroundColor:backgroundColor]; 
} 

最后使用CustomCell在VI ew并设置背景或图像

[[CustomCell appearance] setBackgroundCellColor:[UIColor redColor]]; 
+1

甜!我会试试看。感谢您的提示! – 2013-03-06 21:37:47

+0

我试过了,它没有解决。我试图改变UITableViewCell的backgroundView,但它并没有将样式应用于所有的UITableViewCells。 – 2013-03-07 20:03:10

+0

@johndoe试试这个[教程](http://46media.net/xcode-turorials/customize-the-appearance-of-uitableview-and-uitableviewcell-in-xcode-4/)它有工作代码与视频 – nsgulliver 2013-03-07 20:40:26