2011-07-26 68 views
0

所以我必须直接从笔尖文件加载细胞:selectedBackgroundView在自定义的UITableViewCell

NSArray *cellContents = [[NSBundle mainBundle] loadNibNamed:@"ResultsTableViewCell" owner:self options:nil]; 
     cell = [cellContents objectAtIndex:0]; 

在笔尖文件,有一个UITableViewCell和两个UIViews,每个都有自己的子视图。 Strucuture:

-TableViewCell 
    Label 1 
    Label 2 
-UIView A 
    UIView a 
    UIView b 
    UIImageView c 
    UIImageView d 
-UIView B 
    UIView e 
    UIView f 
    UIImageView g 
    UIImageView h 

所以UIView的A被连接到backgroundView属性为的UITableViewCell,UIView的B连接到selectedBackgroundView属性。

问题: 因此,为什么A显示了良好的细胞作为它的所有子视图的背景,并且B中的UIImageViews工作正常,但B中的UIViews并不显示,但是我改变了它?谢谢!

+0

我有这个确切的问题。你有没有找到解决方案? – jimmyg

回答

0

这是我发现我的情况。当我登录了UIView的背景颜色的RGB和α,我发现他们都得到设置为0,选择单元格时:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    CGFloat red, green, blue, alpha; 
    [viewE.backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha]; 
    NSLog(@"Selected: Red=%f, Green=%f, Blue=%f, alpha=%f", red, green, blue, alpha); 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    CGFloat red, green, blue, alpha; 
    [viewE.backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha]; 
    NSLog(@"Deslected: Red=%f, Green=%f, Blue=%f, alpha=%f", red, green, blue, alpha); 
} 

最初,没有被选中。点击表中的一行,日志显示:

Selected: Red=0.000000, Blue=0.000000, Green=0.000000, alpha=0.000000 

和我的子视图不会出现(注意Alpha = 0)。有趣的是,当我登录viewE.alpha,这表明它是1

再次点击该行取消对它的选择:

Deselected: Red=0.667000, Blue=0.776000, Green=0.220000, alpha=1.000000 

这就是我的期望。

我的表格单元格是一个自定义的UITableViewCell。所以在我的自定义UITableViewCell类,我添加了这个代码:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    UIColor *saveColor = self.viewE.backgroundColor; 
    [super setSelected:selected animated:animated]; 
    if (selected) 
     self.viewE.backgroundColor = saveColor; 
} 

现在所选单元格时viewE出现的子视图。

好像代码这里补偿不应该摆在首位已经存在的条件,但它的作品!