2012-01-31 88 views
1

有人可以告诉我为什么这不工作正确吗?UIView背景颜色不生效

我的表格视图单元格中有以下代码行,用于tableView:didSelectAtIndexRowPath:方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[tableView viewWithTag:199]removeFromSuperview]; 

    CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size; 

    UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)]; 

    [subSelectionView setBackgroundColor:[UIColor blueColor]]; 

    subSelectionView.layer.borderColor = [UIColor grayColor].CGColor; 
    subSelectionView.layer.borderWidth = 1; 

    subSelectionView.tag = 199;  

    [[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView]; 
} 

注意代码:

[subSelectionView setBackgroundColor:[UIColor blueColor]]; 

很明显,我想改变,我加入到UITableViewCell子视图的颜色,而是它为什么不工作?

+0

确保'subSelectionView'不被表格视图单元格的内容视图覆盖,因为这似乎是发生了什么。 – Costique 2012-01-31 05:10:04

回答

2

强制重装该表添加子视图在桌子上后

[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView]; 

[tableview reloadData]; 
+0

谢谢..它适用于我..:D很高兴你回答了两个.. – Aldee 2012-01-31 05:13:36

1

以下行添加到方法的开始

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 

所以乌尔法将

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    [[tableView viewWithTag:199]removeFromSuperview]; 
    .... 
}